1

Lots of folks use redis for simple key/value cache store. Sometimes you just want to kill off a subset of your keys.

The common question I see everywhere is "How do I do a DEL prefix*".

For various reasons, Redis doesn't currently have this functionality and I get the vibe they have strong fundamental position against such a feature it will likely never happen.

As far as I know, there are a number of work-arounds like this piping fun on the command line:

 redis-cli  KEYS "prefix:*" | xargs redis-cli  DEL 

Or using an EVAL:

 EVAL "local keys = redis.call('keys', ARGV[1]) \n for i=1,#keys,5000 do \n redis.call('del', unpack(keys, i, math.min(i+4999, #keys))) \n end \n return keys" 0 prefix:*

However, I've learned from folks on the Redis github project that using KEYS * is expensive and bad practice--with the suggestion that it's to be avoided on production systems: https://github.com/antirez/redis/issues/2042

The indication is a better way to match key patterns is via SCAN

 SCAN 0 MATCH prefix:* 

Does anyone know a way within redis to use SCAN in conjunction with DEL to achieve at least a mock of the holy grail, a simple DEL prefix:*?

Or must you use SCAN in a separate program/shell script, then parse the results and issue DEL on the keys in a loop or such?

Ray
  • 40,256
  • 21
  • 101
  • 138

0 Answers0