# RENAMENX

> Rename a key only if the new name is not taken.

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

```
RENAMENX key newkey
```

- Group: Keys
- Complexity: O(1)
- HotKV Cloud: all plans
- On-Premise: no license needed

`RENAMENX` renames `key` to `newkey` only when `newkey` does not exist. The
value keeps its time to live. Renaming a key to its own name is treated as
"the name is taken" and replies `0`.

In a cluster, `key` and `newkey` must hash to the same slot.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Renamed | Integer `1` | Integer `1` |
| `newkey` already exists | Integer `0` | Integer `0` |
| `key` does not exist | Error `ERR no such key` | Error |

## Examples

```
127.0.0.1:6379> MSET a "first" b "second"
OK
127.0.0.1:6379> RENAMENX a b
(integer) 0
127.0.0.1:6379> RENAMENX a c
(integer) 1
127.0.0.1:6379> GET c
"first"
```

## Compatibility

Same as Redis 7 and Valkey 8.

## See also

[RENAME](https://docs.hotkv.com/commands/rename/index.md), [COPY](https://docs.hotkv.com/commands/copy/index.md)