# BUSYKEY: Target key already exists

> A command that restores or moves a key found the target name occupied: RESTORE, MIGRATE and friends refuse without REPLACE.

Source: https://sqlly.app/errors/redis/busykey
Last updated: 2026-11-30
License: Text and data [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) (credit SQLly, sqlly.app); code and SQL snippets [MIT](https://opensource.org/license/mit). [Details](https://sqlly.app/license).
Note: Quoted error message text belongs to its database vendor.

- Error prefix: `BUSYKEY`
- Category: Memory and persistence

Error message:

```text
BUSYKEY Target key name already exists.
```

## What it means

BUSYKEY comes from commands that create a key from elsewhere: RESTORE writes a serialized value, and MIGRATE moves a key between instances. Both refuse to overwrite an existing target unless you pass REPLACE, and the refusal is this error. It is a collision check, not corruption: the target key may be live data, and the decision is yours.

Pass REPLACE when the source is authoritative, such as a migration replaying state; rename the target when both keys matter. Related commands behave differently on purpose: COPY without REPLACE simply returns 0 when the target exists, so scripts ported between the two need adjusting.

## Common causes

- RESTORE was called on a key name that already exists, without REPLACE.
- MIGRATE moved a key to an instance where the same name had already been created.
- A migration or cache-warming job re-ran without idempotency and replayed keys that already landed.

## How to fix it

1. Check the target before the operation:

   ```sql
   EXISTS target-key
   TYPE target-key
   ```
2. Pass REPLACE when the source of truth is the key being moved or restored:

   ```sql
   MIGRATE 127.0.0.1 6380 mykey 0 5000 REPLACE
   ```
3. When both keys are real data, rename the target first instead of overwriting it:

   ```sql
   RENAME target-key target-key-backup
   ```

## Reproduce it safely

```sql
redis-server --port 7309 --save '' &
redis-server --port 7310 --save '' &
redis-cli -p 7309 SET mykey hello
redis-cli -p 7310 SET mykey occupied
redis-cli -p 7309 MIGRATE 127.0.0.1 7310 mykey 0 5000
# (error) BUSYKEY Target key name already exists.
redis-cli -p 7309 MIGRATE 127.0.0.1 7310 mykey 0 5000 REPLACE
# OK
```

## How SQLly helps

The full BUSYKEY reply lands in SQLly's results pane, and EXISTS and MIGRATE run from the same editor against both instances, so a collision is checked in place instead of being scripted around.

## Questions

### Is BUSYKEY like a duplicate key error?

In spirit, yes: the name you tried to create is taken. The difference is which commands can raise it, RESTORE and MIGRATE, while SET overwrites silently.

### Did MIGRATE lose the key when this happened?

No. MIGRATE is atomic from the source's point of view: on BUSYKEY without REPLACE, the key stays on the source untouched.

## Related errors

- [`23505`](https://sqlly.app/errors/postgresql/23505): PostgreSQL: duplicate key violates unique constraint
- [`1062`](https://sqlly.app/errors/mysql/1062): MySQL: Duplicate entry for key
- [`BUSYGROUP`](https://sqlly.app/errors/redis/busygroup): Consumer group already exists

The code and message text are from the [Redis documentation and source code](https://redis.io/docs/latest/).
