Easy Tutorial
❮ Sorted Sets Zremrangebyscore Java Environment Setup ❯

Redis Rename Command

Redis Keys

The Redis Rename command is used to change 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 on success, or an error if the rename fails.

An error is returned if OLD_KEY_NAME and NEW_KEY_NAME are the same, or if OLD_KEY_NAME does not exist.

If NEW_KEY_NAME already exists, the RENAME command will overwrite the old value.

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


# RENAME overwrites the old newkey when newkey already exists

redis> SET pc "lenovo"
OK

redis> SET personal_computer "dell"
OK

redis> RENAME pc personal_computer
OK

redis> GET pc
(nil)

redis:1> GET personal_computer      # The original value 'dell' is overwritten
"lenovo"

Redis Keys

❮ Sorted Sets Zremrangebyscore Java Environment Setup ❯