w3resource

Redis Keys: RENAME

RENAME key newkey

The Redis RENAME command is used to change the name of a key to newkey.

  • It returns an error when the source and destination names are the same, or when a key does not exist.
  • If newkey already exists it is overwritten, when this happens RENAME executes an implicit DEL operation, so if the deleted key contains a very big value

Syntax:

RENAME OLD_KEY_NAME NEW_KEY_NAME

Available since

1.0.0.

Return Value

String reply OK or error.

Return Value Type

String

Example - 1: Redis RENAME

First, create some keys in redis and set some values in it.

127.0.0.1:6379> SET key "PHP"
OK
127.0.0.1:6379> RENAME key JavaScript
OK
127.0.0.1:6379> GET JavaScript
"PHP"

Example - 2: Redis RENAME

127.0.0.1:6379> MSET key "PHP" key1 "JavaScript" key2 "Redis"
OK
127.0.0.1:6379> RENAME key key1
OK
127.0.0.1:6379> GET key1
"PHP"

Previous: RANDOMKEY
Next: RENAMENX



Follow us on Facebook and Twitter for latest update.