SWAPDB
SWAPDB index1 index2
- Group
- Server
- Complexity
- O(1)
- Arity
- 3 (exactly, counting the command name)
- Flags
- write, fast
- 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 keys in either
database fail at EXEC, since the watched data may have changed.
Return value
Section titled “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
Section titled “Examples”127.0.0.1:6379> SELECT 1OK127.0.0.1:6379[1]> SET catalog:version "v2"OK127.0.0.1:6379[1]> SWAPDB 0 1OK127.0.0.1:6379[1]> SELECT 0OK127.0.0.1:6379> GET catalog:version"v2"Compatibility
Section titled “Compatibility”Same replies as Redis 7 and Valkey 8.
See also
Section titled “See also”In a cluster: Keys, slots and commands in a cluster
Was this page helpful?
Thanks for the feedback.