Redis RENAME Command
The Redis RENAME command is used to modify the name of a key.
Syntax
The basic syntax of the Redis RENAME command is as follows:
redis 127.0.0.1:6379> RENAME OLD_KEY_NAME NEW_KEY_NAME
Available versions
>= 1.0.0
Return value
Returns OK when the rename succeeds, and returns an error when it fails.
When OLD_KEY_NAME and NEW_KEY_NAME are the same, or when OLD_KEY_NAME does not exist, an error is returned. When NEW_KEY_NAME already exists, the RENAME command will overwrite the old value.
Examples
Examples
# key exists and newkey does not exist
redis> SET message "hello world"
OK
redis> RENAME message greeting
OK
redis> EXISTS message # message no longer exists
(integer) 0
redis> EXISTS greeting # greeting takes its place
(integer) 1
# returns an error when the key does not exist
redis> RENAME fake_key never_exists
(error) ERR no such key
# when newkey already exists, RENAME will overwrite the old newkey
redis> SET pc "lenovo"
OK
redis> SET personal_computer "dell"
OK
redis> RENAME pc personal_computer
OK
redis> GET pc
(nil)
redis> GET personal_computer # the original value dell was overwritten
"lenovo"
redis> SET message "hello world"
OK
redis> RENAME message greeting
OK
redis> EXISTS message # message no longer exists
(integer) 0
redis> EXISTS greeting # greeting takes its place
(integer) 1
# returns an error when the key does not exist
redis> RENAME fake_key never_exists
(error) ERR no such key
# when newkey already exists, RENAME will overwrite the old newkey
redis> SET pc "lenovo"
OK
redis> SET personal_computer "dell"
OK
redis> RENAME pc personal_computer
OK
redis> GET pc
(nil)
redis> GET personal_computer # the original value dell was overwritten
"lenovo"
Other extensions
Redis keys