How to rename a key in redis datastore – Redis RENAME | RENAMENX

6 days ago Lalit Bhagtani 0

In this tutorial, we will learn about how to rename a key in redis datastore, by using redis RENAME and RENAMENX command.

RENAME Command :-

The RENAME command, rename the key from old name to new name. if a key with new name already exists, it will be overwritten and key with old name will be deleted by using implicit DEL command otherwise key with old name will be changed to new name. The syntax of redis RENAME command is as follows :-

Syntax :-

redis host:post> RENAME <old name> <new name>

Output :-

- string reply, if key is renamed to new name
- error if key with old name does not exists

Example :-

Redis RENAME

RENAMENX Command :-

The RENAMENX command, rename the key from old name to new name. if a key with new name already exists, 0 will be returned otherwise key with old name will be changed to new name. The syntax of redis RENAMENX command is as follows :-

Syntax :-

redis host:post> RENAMENX <old name> <new name>

Output :-

- 1 if key is renamed to new name.
- 0 if key with new name already exists.
- error if key with old name does not exists

Example :-

Redis RENAMENX

References :-

  1. RENAME Command Docs
  2. RENAMENX Command Docs

That’s all for how to rename a key stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.