redis delete all keys

Reading Time: < 1 minutes

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

Let’s ping our Redis instance

   
redis-cli -h myhost.com -p 6379 ping
   

Set some keys

   
redis-cli -h myhost.com -p 6379 SET dev1 "val1"
redis-cli -h myhost.com -p 6379 SET dev2 "val2"
redis-cli -h myhost.com -p 6379 SET dev3 "val3"
   

Get one key

   
redis-cli -h myhost.com -p 6379 KEYS dev1 
   

Delete one key

   
redis-cli -h myhost.com -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 myhost.com -p 6379 | awk '{print $1}'`
  do echo KEYS $key
done | redis-cli -c -h myhost.com -p 6379
   

And then when you are sure, go ahead with the deletion

   
for key in `echo 'KEYS dev*' | redis-cli -c -h myhost.com -p 6379 | awk '{print $1}'`
  do echo DEL $key
done | redis-cli -c -h myhost.com -p 6379
   

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

Photo by Markus Winkler on Unsplash


About the author

Andrés Canavesi
Andrés Canavesi

Software Engineer with 15+ experience in software development, specialized in Salesforce, Java and Node.js.


Related posts


Leave a Reply

%d bloggers like this: