# COPY

> Copy the value of a key to another key, optionally in another database.

URL: https://docs.hotkv.com/commands/copy/

```
COPY source destination [DB destination-db] [REPLACE]
```

- Group: Keys
- Complexity: O(N) where N is the size of the value copied
- HotKV Cloud: all plans
- On-Premise: no license needed

`COPY` duplicates the value at `source` into `destination`, including its time
to live. The source is left as it is. By default nothing is copied when
`destination` already exists.

## Options

| Option | Effect |
| --- | --- |
| `DB destination-db` | Write the copy into this database instead of the selected one. On a cluster node the default `cluster-databases 1` refuses another database (`ERR Copying to another database is not allowed in cluster mode`); with a higher `cluster-databases` it works between the node's databases. |
| `REPLACE` | Overwrite `destination` if it exists. |

The options can come in any order.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Copied | Integer `1` | Integer `1` |
| `source` does not exist, or `destination` exists and `REPLACE` was not given | Integer `0` | Integer `0` |

Errors:

- `ERR source and destination objects are the same` when both names and
  databases match.
- `ERR DB index is out of range` for a database that does not exist.
- `ERR value is not an integer or out of range` when the database index is not
  an integer.
- `ERR syntax error` for any other argument.

## Examples

```
127.0.0.1:6379> SET config:v1 "max=10"
OK
127.0.0.1:6379> COPY config:v1 config:v2
(integer) 1
127.0.0.1:6379> COPY config:v1 config:v2
(integer) 0
127.0.0.1:6379> COPY config:v1 config:v2 REPLACE
(integer) 1
127.0.0.1:6379> COPY config:v1 config:v1 DB 1
(integer) 1
```

## Compatibility

Same as Redis 7 and Valkey 8.

## See also

[RENAME](https://docs.hotkv.com/commands/rename/index.md), [MOVE](https://docs.hotkv.com/commands/move/index.md),
[DUMP](https://docs.hotkv.com/commands/dump/index.md), [RESTORE](https://docs.hotkv.com/commands/restore/index.md)

In a cluster: [Keys, slots and commands in a cluster](https://docs.hotkv.com/clustering/keys-and-commands/index.md#databases)