0

I need to use NodeJS to delete all records in Redis that match a prefix / wildcard. I'm using this package: https://github.com/NodeRedis/node_redis

I have seen several solutions available to delete Redis keys that feature a prefix and a wildcard e.g.

KEYS "web.*"

However, all of these solutions rely on using the KEYS command which is not suitable for production due to it locking up the server. I believe the correct solution involves the SCAN command (and perhaps others), but I don't fully understand how it works.

I'm looking for a simple JS function that allows me to pass in a string containing the prefix and wildcard e.g. "web.*" and all "keys" matching keys will be deleted.

Many thanks!

John1984
  • 917
  • 2
  • 13
  • 23

1 Answers1

1

Redis is single threaded, so it's going to block regardless. Scan returns the result in an iterative fashion, so while it still blocks, it wont block for as long given it's returning a subset of the overall result.
https://redis.io/commands/scan

There's a scan solution here that may or may not work for you depending on whether you need it to be atomic or not. How to atomically delete keys matching a pattern using Redis

Community
  • 1
  • 1
davissp14
  • 765
  • 4
  • 13
  • How do you call "EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 prefix:*" using node_redis? Thanks! – John1984 Nov 30 '16 at 12:45
  • 1
    There's no great documentation on it. However, if you dig through the node_redis source you can find some examples. https://github.com/NodeRedis/node_redis/blob/ff9b727609ea125919828f7373e40082fd432eec/test/commands/eval.spec.js#L135 – davissp14 Nov 30 '16 at 16:07