Delete all keys by a prefix in Redis cluster

Posted on Aug 04, 2020

featured image
Delete all the keys by a given prefix in a Redis cluster.

Let's ping our Redis instance

redis-cli -h -p 6379 ping
Set some keys
redis-cli -h -p 6379 SET dev1 "val1"
redis-cli -h -p 6379 SET dev2 "val2"
redis-cli -h -p 6379 SET dev3 "val3"
Get one key
redis-cli -h -p 6379 KEYS dev1 
Delete one key
redis-cli -h -p 6379 DEL dev1 
Now let's go with our massive deletion algorithm but before making any deletion let's test the algorithm without making changes.
for key in `echo 'KEYS dev*' | redis-cli -c -h -p 6379 | awk '{print $1}'`
  do echo KEYS $key
done | redis-cli -c -h -p 6379
And then when you are sure, go ahead with the deletion
for key in `echo 'KEYS dev*' | redis-cli -c -h -p 6379 | awk '{print $1}'`
  do echo DEL $key
done | redis-cli -c -h -p 6379

In case you are not using a cluster just remove the -c options from redis-cli

Photo by u j e s h on Unsplash

redis sysamdin
Side Widget
You can put anything you want inside of these side widgets. They are easy to use, and feature the new Bootstrap 4 card containers!