I'm using ioredis with express (nodejs) I know that there is a way to delete keys by pattern like this:
redis-cli KEYS "sample_pattern:*" | xargs redis-cli DEL
However, is there a way to do that using ioredis instead?
The most straightforward way to delete keys by pattern is using keys command to get the keys matching the pattern and then deleting them one by one, which is similar to the command line example you provided. Here's an example implemented with ioredis: var Redis = require('ioredis'); var redis = new Redis(); redis.
To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.
First select your keys by pattern, then remove them by del
method.
const keys = await ioredis.keys('PATTERN:*');
await ioredis.del(keys);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With