简体   繁体   中英

Delete redis key matching pattern using ruby

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

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

Try this -

 $redis.del(datetime_filter_key)

and follow following approach -

In redis, how do i remove keys?

You could use:

Rails.cache.redis.keys.grep(/pattern/).each do |k|
  Rails.cache.redis.del(k)
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM