How to remove all the keys in a Redis OSS Cluster?

Last updated 27, Apr 2024

Goal

Learn how to remove all the keys in a Redis Cluster

Solution

You can remove all the keys in a Redis OSS Cluster (or a cluster built with Redis Stack servers) by connecting to every master shard using the redis-cli command-line client and executing the command:

FLUSHALL

You can list the masters using the command:

CLUSTER SLOTS

Data deletion on all the shards is delegated to client libraries when using the corresponding API. An example using redis-py follows:

from redis.cluster import RedisCluster
from redis.cluster import ClusterNode

startup_nodes = [ClusterNode('127.0.0.1', 30001),
                ClusterNode('127.0.0.1', 30002),
                ClusterNode('127.0.0.1', 30003)]

rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)

print(rc.flushall())

The execution of this example reports the outcome of the operation on the multiple master shards.

{'127.0.0.1:30004': True, '127.0.0.1:30005': True, '127.0.0.1:30006': True}

References