2

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

Naveen Kumar
  • 2,570
  • 3
  • 20
  • 22

3 Answers3

6

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
antirez
  • 18,314
  • 5
  • 50
  • 44
4

Try this -

 $redis.del(datetime_filter_key)

and follow following approach -

In redis, how do i remove keys?

Community
  • 1
  • 1
Amit Suroliya
  • 1,515
  • 1
  • 11
  • 21
1

You could use:

Rails.cache.redis.keys.grep(/pattern/).each do |k|
  Rails.cache.redis.del(k)
end
builder-7000
  • 7,131
  • 3
  • 19
  • 43