Upstash Documentation

RENAME

Rename a key.
2 min read

Use RENAME to give an existing key a new name.

The value moves with the key and so does its remaining time to live, and the operation is atomic, so no client ever sees both names or neither. If a key with the destination name already exists it is overwritten and its old value is deleted. Renaming a key that does not exist returns an error.

Use RENAMENX when the destination must not be overwritten. A common pattern is to build a replacement value under a temporary key and then rename it over the live key, which swaps the data in one atomic step.

Syntax#

Arguments#

ArgumentRequiredRepeatableDescription
<key>YesNoRedis key targeted by the command.
<newkey>YesNoRedis key used as newkey.

Response#

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

ProtocolReply
RESP2Simple string OK
RESP3Simple string OK
Note

Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.

Examples#

TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.

Redis CLI
@upstash/redis
upstash_redis
ioredis
node-redis
redis-py
go-redis
jedis
redis-rs