Kyle
Kyle

Reputation: 4366

Delete multiple Redis keys based on pattern without bash

I am trying to remove multiple keys from a Redis instance based on a specified key pattern. However, unique to many of the other answers I've seen on SO, I need to do this entirely within the Redis console without the use of any other scripting or Bash. The reason being that I need to execute this from within the Console functionality of the Azure Cache for Redis portal. I am able to select the keys I want using SCAN 0 MATCH *mypattern* but, I don't know of any way to "pipe" this to the DEL command.

Upvotes: 0

Views: 1267

Answers (1)

Kyle
Kyle

Reputation: 4366

In case this is helpful to anyone else, here is the answer I found that worked for me.

EVAL "return redis.call('del', unpack(redis.call('scan', 0, ‘MATCH', ARGV[1])[2]))" 0 *mypattern*

The inner redis.call('scan') command finds all keys matching *mypattern* (any key with "mypattern" anywhere in it). The second index position of the return value of this command is the list of the keys which is then unpacked and passed to the outer redis.call('del') command which deletes the objects from the cache.

Note that this will return an error if there are no keys matching the pattern. This was ok for my use case so I did not explore it further, but there may be a way around this.

Upvotes: 1

Related Questions