RENAMENX(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’… RENAMENX β€” the glow-up your keys deserve (but only if the name's free)

okay bestie, welcome to the Valkey Command Manual, page RENAMENX(3valkey) 🌸 this is the one you pull up when you want to rename a key but you're not about to steamroll some other key that already claimed that name. very demure, very mindful, very respectful of boundaries ✨


✨ NAME ✨

renamenx β€” renames a key, but ONLY if the target key name doesn't already exist. it's giving "i'll move in but only if nobody's living there already" πŸ πŸ’–


πŸŽ€ SYNOPSIS πŸŽ€

RENAMENX key newkey

that's it. two args. no drama. iconic.


πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the vibe: RENAMENX renames key to newkey if newkey doesn't exist yet. that's the whole point, that's the "NX" in the name, no cap πŸ’…

⚠️ and if key doesn't exist at all? it returns an error. like, girl, you can't rename a key that was never even here 😭

and okay, one kinda important thing if you're running in Cluster mode β€” both key and newkey have to live in the same hash slot. so in practice? only keys that share the same hash tag can be reliably renamed in cluster. plan accordingly bestie 🧚


πŸ’Œ REPLY πŸ’Œ

you get one of these two, both are valkey-protocol(7) Integer replies:

  • 1 β€” yasss, key was renamed to newkey πŸŽ‰
  • 0 β€” nope, newkey already exists, no rename happened πŸ™ˆ

⚑ COMPLEXITY ⚑

O(1) β€” constant time. she is fast, she is efficient, she does not make you wait 🫢


πŸ‘‘ ACL CATEGORIES πŸ‘‘

@fast @keyspace @write


🌈 HISTORY 🌈

  • Available since: 1.0.0 β€” an OG, a legend, been here since the beginning πŸ‘‘
  • Changed in 3.2.0: the command no longer returns an error when source and destination names are the same. so renaming a key to itself? chill now. no more tantrums πŸ’€

πŸ“ EXAMPLES πŸ“

watch her work:

127.0.0.1:6379> SET mykey "Hello"
OK
127.0.0.1:6379> SET myotherkey "World"
OK
127.0.0.1:6379> RENAMENX mykey myotherkey
(integer) 0
127.0.0.1:6379> GET myotherkey
"World"

see what happened? myotherkey was already taken, so we got that big fat (integer) 0, and GET myotherkey still says "World". the original key stays untouched. boundaries = respected 🫢 slay


πŸ”₯ SEE ALSO πŸ”₯

the whole friend group, go say hi:


k bye, ily, go rename something responsibly πŸ’œ this page is from version 9.0.0, dated 2025-10-21, and it's the RENAMENX(3valkey) page β€” periodt. πŸŽ€βœ¨

πŸ“¦ where this page comes from