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 Version
= 1.0.0
Return Value
Returns OK on success, or an error on failure.
Returns an error when OLD_KEY_NAME and NEW_KEY_NAME are the same, or when OLD_KEY_NAME does not exist. When NEW_KEY_NAME already exists, the RENAME command overwrites the old value.
Example
Example
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 key does not exist
redis
RENAME fake_key never_exists
(
error
)
ERR no such key
When newkey already exists, RENAME overwrites 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 Key