What's new in Valkey 9.0? Discover new features and improvements. Read the announcement.

RENAMENX

Renames a key only when the target key name doesn't exist.

Usage
RENAMENX key newkey
Complexity
O(1)
Since
1.0.0
ACL Categories
@fast, @keyspace, @write
Command flags
WRITE, FAST

Renames key to newkey if newkey does not yet exist. It returns an error when key does not exist.

In Cluster mode, both key and newkey must be in the same hash slot, meaning that in practice only keys that have the same hash tag can be reliably renamed in cluster.

Examples

127.0.0.1:6379> SET mykey "Hello"
OK
127.0.0.1:6379> SET myotherkey "World"
OK
127.0.0.1:6379> RENAMENX mykey myotherkey
(integer) 0
127.0.0.1:6379> GET myotherkey
"World"

Replies

RESP2

One of the following:

RESP3

One of the following:

History

  • 3.2.0: The command no longer returns an error when source and destination names are the same.