Target key name already exists
Production Risk
Low. The server is correctly preventing accidental data loss.
This error occurs with commands like MIGRATE or RESTORE when the target key already exists on the destination and the command was not instructed to replace it. It's a safety mechanism to prevent accidental data overwrites.
- 1Attempting to `RESTORE` a key onto a destination where that key name is already in use.
- 2Running a `MIGRATE` command for a key that has already been successfully migrated but not deleted from the source.
- 3A race condition where two processes try to create the same key on the destination instance.
A client tries to restore a key 'mykey' but 'mykey' already exists on the server.
SET mykey "initial value" # DUMP mykey produces some payload, e.g., "..." RESTORE mykey 0 "...payload..."
expected output
(error) BUSYKEY Target key name already exists.
Fix 1
Use the `REPLACE` modifier
WHEN When you intentionally want to overwrite the destination key
RESTORE mykey 0 "...payload..." REPLACE
Why this works
The `REPLACE` option explicitly tells the `RESTORE` or `MIGRATE` command that it is allowed to delete the existing key at the destination before proceeding.
Fix 2
Delete the key from the destination before running the command
WHEN When you want to ensure a clean slate before the operation
DEL mykey RESTORE mykey 0 "...payload..."
Why this works
By manually deleting the key, you ensure the precondition for the command (the key not existing) is met.
✕ Always use `REPLACE` without checking
Blindly using `REPLACE` can lead to unintentional data loss if the key on the destination had important data that was different from what you are restoring.
Older versions of `RESTORE` would not return this error and would simply fail or overwrite, depending on the exact version. BUSYKEY provides clearer semantics.
Key creation and replacement logic for restore/migrate.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev