w3resource

Redis Keys: RENAME

Redis RENAME Command

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.

Here are some common uses and scenarios for the "RENAME" command:

  • Key Renaming: Change the name of a key to a new name.
  • Data Organization: Maintain consistent naming conventions for keys.
  • Migration: Update key names during data migrations or schema changes.
  • Atomic Operation: Perform renaming atomically without risking data loss.

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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-rename-key-newkey.php