Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

CLUSTER FLUSHSLOT

Last updated
CLUSTER FLUSHSLOT slot [ASYNC | SYNC]
Group
Cluster
Arity
-3 (at least 3 words, counting the command name)
HotKV Cloud
Not available
On-Premise
no license needed

CLUSTER FLUSHSLOT deletes all the keys of one hash slot from the node it is sent to. It is a way to empty a slot without touching the rest of the node, for example to drop the data of one hash tag. Send it to the primary that serves the slot: the node deletes the keys it holds for that slot, and replies OK without checking whether it serves the slot, so on any other node it deletes nothing and still replies OK.

The command deletes the slot’s keys in every database of the node (see cluster-databases), sends the deletions to the replicas, and publishes a del keyspace event for each key when keyspace notifications are on. The entries of the AI engines that belong to the slot (a cache namespace, for instance) are dropped too. A replica refuses the command; it receives the deletions from its primary.

Option Effect
ASYNC Accepted, as in Valkey. The keys were gone when the reply arrived.
SYNC Accepted, as in Valkey. The keys were gone when the reply arrived.

Simple string OK in RESP2 and RESP3, also when the slot holds no keys. Errors:

Error Cause
ERR Invalid or out of range slot The slot is not a number from 0 to 16383.
ERR syntax error The second option is not ASYNC or SYNC.
ERR wrong number of arguments for 'cluster|flushslot' command No slot given.
READONLY You can't write against a read only replica. Sent to a replica.

The key {t} is in slot 15891. Two keys are in database 0 and one in database 1 of a node that has cluster-databases 2; its replica has them too. A subscriber on the primary listens for del events:

127.0.0.1:7000> CLUSTER COUNTKEYSINSLOT 15891
(integer) 2
127.0.0.1:7000> CLUSTER FLUSHSLOT 15891
OK
127.0.0.1:7000> CLUSTER COUNTKEYSINSLOT 15891
(integer) 0
127.0.0.1:7000> SELECT 1
OK
127.0.0.1:7000[1]> CLUSTER COUNTKEYSINSLOT 15891
(integer) 0
127.0.0.1:7000[1]> DBSIZE
(integer) 0
$ redis-cli --no-raw -p 7000 PSUBSCRIBE "__keyevent@*__:del"
1) "psubscribe"
2) "__keyevent@*__:del"
3) (integer) 1
1) "pmessage"
2) "__keyevent@*__:del"
3) "__keyevent@0__:del"
4) "{t}a"
1) "pmessage"
2) "__keyevent@*__:del"
3) "__keyevent@0__:del"
4) "{t}b"
1) "pmessage"
2) "__keyevent@*__:del"
3) "__keyevent@1__:del"
4) "{t}c"

The replica, which held {t}a before, no longer has it:

127.0.0.1:7003> READONLY
OK
127.0.0.1:7003> GET {t}a
"1"
127.0.0.1:7003> GET {t}a
(nil)

(The two GETs were run before and after the flush.) ASYNC and SYNC, and the errors:

127.0.0.1:7000> CLUSTER FLUSHSLOT 15891 ASYNC
OK
127.0.0.1:7000> CLUSTER FLUSHSLOT 15891 SYNC
OK
127.0.0.1:7000> CLUSTER FLUSHSLOT 99999
(error) ERR Invalid or out of range slot
127.0.0.1:7000> CLUSTER FLUSHSLOT 15891 LATER
(error) ERR syntax error
127.0.0.1:7000> CLUSTER FLUSHSLOT
(error) ERR wrong number of arguments for 'cluster|flushslot' command
127.0.0.1:7001> CLUSTER FLUSHSLOT 15891
OK
127.0.0.1:7003> CLUSTER FLUSHSLOT 15891
(error) READONLY You can't write against a read only replica.

The last two commands went to a node that does not serve slot 15891 (port 7001, nothing deleted) and to a replica.

The command follows Valkey 9’s CLUSTER FLUSHSLOT. Redis has SFLUSH for the same purpose.

SFLUSH, CLUSTER COUNTKEYSINSLOT, CLUSTER FLUSHSLOTS, FLUSHALL