# GETSET

> Set a string value and return the value it replaced.

URL: https://docs.hotkv.com/commands/getset/

```
GETSET key value
```

- Group: String
- Complexity: O(1)
- HotKV Cloud: all plans
- On-Premise: no license needed
- Deprecated: use SET key value GET instead

`GETSET` stores `value` at `key` and returns the previous string, in one step:
two clients calling `GETSET` on the same key never both see the same old
value. Like a plain [SET](https://docs.hotkv.com/commands/set/index.md), it clears any time to live. New
code can use `SET key value GET`, which does the same and also accepts the
other `SET` options.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| The key held a string | Bulk string: the old value | Bulk string |
| The key did not exist | Nil bulk string | Null |

`GETSET` replies with a `WRONGTYPE` error, and stores nothing, when the key
holds a value that is not a string.

## Examples

```
127.0.0.1:6379> GETSET status "running"
(nil)
127.0.0.1:6379> GETSET status "stopped"
"running"
127.0.0.1:6379> GET status
"stopped"
```

## Compatibility

Same as Redis 7 and Valkey 8, where it is also deprecated in favour of
`SET ... GET`.

## See also

[SET](https://docs.hotkv.com/commands/set/index.md), [GET](https://docs.hotkv.com/commands/get/index.md), [GETDEL](https://docs.hotkv.com/commands/getdel/index.md)