Easy Tutorial
❮ Server Flushall Server Config Set ❯

Redis Renamenx Command

Redis Keys

The Redis Renamenx command is used to rename a key only if the new key does not exist.

Syntax

The basic syntax for the redis Renamenx command is as follows:

redis 127.0.0.1:6379> RENAMENX OLD_KEY_NAME NEW_KEY_NAME

Available Versions

= 1.0.0

Return Value

Returns 1 if the renaming is successful. Returns 0 if the NEW_KEY_NAME already exists.

Examples

# newkey does not exist, renaming successful

redis> SET player "MPlyaer"
OK

redis> EXISTS best_player
(integer) 0

redis> RENAMENX player best_player
(integer) 1

# newkey exists, renaming failed

redis> SET animal "bear"
OK

redis> SET favorite_animal "butterfly"
OK

redis> RENAMENX animal favorite_animal
(integer) 0

redis> get animal
"bear"

redis> get favorite_animal
"butterfly"

Redis Keys

❮ Server Flushall Server Config Set ❯