# SWAPDB

> Swap the contents of two logical databases in one step.

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

```
SWAPDB index1 index2
```

- Group: Server
- Complexity: O(1)
- HotKV Cloud: all plans
- On-Premise: no license needed

`SWAPDB` exchanges the data of two databases. Connections using either
database see the other one's data at once, without reconnecting. The swap is
atomic: no client sees a half-swapped state.

A typical use is to rebuild a data set in a spare database, then swap it in
for the live one. Transactions that [WATCH](https://docs.hotkv.com/commands/watch/index.md) keys in either
database fail at `EXEC`, since the watched data may have changed.

## Return value

| Case | Reply (RESP2 and RESP3) |
| --- | --- |
| Swapped (or both indexes are the same) | Simple string `OK` |
| First index not a 32-bit integer | Error `ERR invalid first DB index` |
| Second index not a 32-bit integer | Error `ERR invalid second DB index` |
| An index is negative or not below `databases` | Error `ERR DB index is out of range` |
| Cluster mode, whatever `cluster-databases` is | Error `ERR SWAPDB is not allowed in cluster mode` |

## Examples

```
127.0.0.1:6379> SELECT 1
OK
127.0.0.1:6379[1]> SET catalog:version "v2"
OK
127.0.0.1:6379[1]> SWAPDB 0 1
OK
127.0.0.1:6379[1]> SELECT 0
OK
127.0.0.1:6379> GET catalog:version
"v2"
```

## Compatibility

Same replies as Redis 7 and Valkey 8.

## See also

[SELECT](https://docs.hotkv.com/commands/select/index.md), [MOVE](https://docs.hotkv.com/commands/move/index.md), [FLUSHDB](https://docs.hotkv.com/commands/flushdb/index.md)

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