SoFunction
Updated on 2025-04-14

Solve the redis error message: BUSYKEY Target key name already exists.

Redis ReturnBUSYKEY Target key name already exists.When errors are usually because you are trying to overwrite oneAlready existing keys, but the current operation does not explicitly allow overwrite.

Here are the detailed explanations and solutions:

Cause of error

This error is common in two operations:

  1. RESTOREOrder: Try to backup data (throughDUMPWhen the generated serialized data) is restored to Redis, if the target key already exists and no overwrite option is specified.
  2. Other coverage scenarios: Some operations that require overriding keys (such asCOPYMIGRATE) Overwrite permission is not explicitly enabled.

Solution

Select the corresponding method according to the operation type:

1. When using the RESTORE command

  • reason: By default,RESTOREOverwriting existing keys is not allowed.
  • repair:Add toREPLACEParameter, force overwrite the target key.
# grammarRESTORE key ttl serialized-value [REPLACE]

# Example: Overwrite existing key `mykey`RESTORE mykey 0 \"\\x00\\x03foo\\x06\\x00\\x8f\\xd8\\xc4\\x8d\\x54\\x5d\\x2d\\x9b\" REPLACE

2. When using the COPY command

  • reason:defaultCOPYThe command does not allow overwriting the target key.
  • repair:Add toREPLACEOptions.
# grammarCOPY source destination [DB destination-db] [REPLACE]

# Example: Copy `key1` to the current database and overwrite the key of the same nameCOPY key1 key1 REPLACE

3. When using the MIGRATE command

  • reason: When migrating data to the target Redis instance, if the target key already exists and overwrite is not enabled.
  • repair:Add toREPLACEparameter.
# grammarMIGRATE host port key|\"\" destination-db timeout [COPY | REPLACE]

# Example: Migrate the key `mykey` and overwrite the target key with the same nameMIGRATE 127.0.0.1 6379 mykey 0 5000 REPLACE

Other precautions

  • Confirm whether coverage is required

Overwrite operations permanently delete old data of the target key! Make sure this is your expected behavior.

  • Check if the key really exists

useEXISTS keyThe command verifies that the key exists.

  • Manually delete old keys

If you don't want to useREPLACE, you can manually delete the old key and then perform the operation:

DEL mykey  # Delete the old keyRESTORE mykey 0 \"\\x00\\x03foo...\"  # No need REPLACE

Error example analysis

Suppose that executing the following command throws an error:

# Try to restore data to an existing key `user:100`RESTORE user:100 0 \"\\x00\\x03foo\\x06\\x00\\x8f\\xd8\\xc4\\x8d\\x54\\x5d\\x2d\\x9b\"
  • Cause of erroruser:100Already exists and not usedREPLACE
  • repair: Add at the end of the commandREPLACE

Summarize

  • Core logic: Redis protects existing keys by default to prevent accidental overwriting.
  • Solution: explicitly add it in operations that require overriding keysREPLACEparameter.
  • Preventive measures: In scripts or automation processes, check in advance whether the key exists or is enabled uniformlyREPLACE

The above is personal experience. I hope you can give you a reference and I hope you can support me more.