No such key
Production Risk
Low. The error correctly signals a failed precondition.
This error is returned by certain commands when they are required to operate on a key that does not exist. Note that many commands (like GET or DEL) do not error on a missing key, but return nil or 0 instead.
- 1The key being operated on has expired and been deleted by Redis.
- 2The key was never created, or was created and then deleted by another client.
- 3A typo in the key name being provided to the command.
A client attempts to rename a key that does not exist.
DEL non_existent_key RENAME non_existent_key new_key_name
expected output
(error) ERR no such key
Fix 1
Check for key existence before operating
WHEN Before using commands that require a key to exist
EXISTS mykey # If response is 1, then proceed # RENAME mykey my_new_key
Why this works
The `EXISTS` command allows you to verify a key is present before running a command like `RENAME` or `PERSIST` that would otherwise fail.
Fix 2
Use `RENAMENX` for safer renaming
WHEN You want to rename a key but only if the new key name doesn't already exist.
RENAMENX old_key new_key
Why this works
While `RENAME` fails on a missing source key, `RENAMENX` provides additional safety on the destination key. Combining `EXISTS` with `RENAME` is the complete pattern.
✕ Rely on this error for checking key existence
Most commands don't return this error. The idiomatic way to check if a key exists is with the `EXISTS` command, and for checking a value, a `GET` command returning `nil` is standard.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev