Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete Redis keys matching a pattern

I am using Redis hash set to store data in the following format:

hset b1.b2.b3 name test

Now I want to delete this key so I am using the following format:

del b1.b2.*

But it not working so how I delete the Redis key using a pattern?

like image 479
Suraj Dalvi Avatar asked Jul 21 '26 10:07

Suraj Dalvi


1 Answers

Redis does not provide any method to delete bulk keys. But redis-cli along with xargs can be used to achieve what you are trying to do. See the commands below:

127.0.0.1:6379> hset b1.b2.b3 name test
(integer) 1
127.0.0.1:6379> hgetall b1.b2.b3
1) "name"
2) "test"
$ redis-cli --scan --pattern b1.b2.* | xargs redis-cli del
(integer) 1
$ redis-cli
127.0.0.1:6379> hgetall b1.b2.b3
(empty list or set)

We are scanning redis for a pattern using '--scan' and the output is given to redis-cli again using the xargs method whcih combines all the keys in the scan result and finally we delete all of them using 'del' command.

like image 123
Ankit Sahay Avatar answered Jul 28 '26 18:07

Ankit Sahay