Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete keys matching a certain pattern in redis

How to delete keys matching a certain pattern in redis using redis-cli. I would like to delete all foo's from the following list.

KEYS *

foo:1
foo:2
bar:1
foo:3
bar:2
foo:4
like image 907
Rpj Avatar asked Feb 18 '15 03:02

Rpj


1 Answers

I wanted to delete thousands of keys by pattern after some searches I found these points:

  • if you have more than one db on redis you should determine the database using -n [number]
  • if you have a few keys use del but if there are thousands or millions of keys it's better to use unlink because unlink is non-blocking while del is blocking, for more information visit this page unlink vs del
  • also keys are like del and is blocking

so I used this code to delete keys by pattern:

 redis-cli -n 2 --scan --pattern '[your pattern]' | xargs redis-cli -n 2 unlink 
like image 54
mahdi yousefi Avatar answered Oct 19 '22 08:10

mahdi yousefi