Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ioredis delete all keys by pattern

Tags:

node.js

redis

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?

like image 823
Hoanggia Le Avatar asked Mar 13 '16 09:03

Hoanggia Le


People also ask

How do I delete a key on Ioredis?

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.

How do I get all Redis keys?

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.


1 Answers

First select your keys by pattern, then remove them by del method.

const keys = await ioredis.keys('PATTERN:*');

await ioredis.del(keys);
like image 120
Ka Arj Avatar answered Sep 17 '22 15:09

Ka Arj