Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete redis key matching pattern using ruby

Tags:

ruby

redis

I want to delete all redis keys defined under namespace "datetime_filter" in ruby (maintenance task). How to do this ?

like image 428
Naveen Kumar Avatar asked Mar 26 '26 19:03

Naveen Kumar


2 Answers

The right way to do this if you don't want to block the server is to use the SCAN command. The command will provide you an iterator returning only the keys matching your pattern if you wish (in this case it is appropriate to use the MATCH option for sure). The Ruby script will just have to iterate and delete.

So:

WHILE keys = SCAN MATCH datetime_filter*
    FOREACH key in keys DEL key
like image 191
antirez Avatar answered Apr 01 '26 09:04

antirez


Try this -

 $redis.del(datetime_filter_key)

and follow following approach -

In redis, how do i remove keys?

like image 23
Amit Suroliya Avatar answered Apr 01 '26 10:04

Amit Suroliya