Node-redis: Delete array of keys in redis using node-redis

Created on 29 Sep 2015  路  5Comments  路  Source: NodeRedis/node-redis

I have arrays of keys like ["aaa","bbb","ccc"] so I want to delete all these keys from redis using one command . I donot want to iterate using loop . I read about redis command DEL and on terminal redis-client it works but using nodejs it does not work

Redisclient.del(tokenKeys,function(err,count){
Logger.info("count is ",count)
Logger.error("err is ",err)
})

where tokenKeys=["aaa","bbb","ccc"] , this code is work if I send one key like tokenKeys="aaa"

question

All 5 comments

client.del can take a variable number of arguments. You can use apply to convert your array into arguments (but you'll need to also push on your callback). It would look something like this:

var tokenKeys = ["aaa","bbb","ccc"];

tokenKeys.push(function(err,count){
Logger.info("count is ",count)
Logger.error("err is ",err)
});

Redisclient.del.apply(Redisclient,tokenKeys);

It's a bit weird, but it will get the job without iterating.

@abhaygarg This should work out of the box just as you wrote it. Could you provide more of your code? I guess the error is somewhere in your code. And what version do you use?

@stockholmux this is not the solution to the issue. Actually it is better to use the array notation right away instead of using arguments (they will be rewritten to an array internal).

yeah its working you can directly pass array in Redisclient.del(tokenKeys,function(err,count){
Logger.info("count is ",count)
Logger.error("err is ",err)
})
it delete fine you can pass array in Redisclient.del() it works fine

app.redis.keys('key_*', (err, keys) => {
    keys.forEach(key => {
        app.redis.del(key)
    })
})

@knoxcard FYI - Don't use KEYS - it's dangerous on large production system.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aletorrado picture aletorrado  路  6Comments

strumwolf picture strumwolf  路  4Comments

twappworld picture twappworld  路  7Comments

dotSlashLu picture dotSlashLu  路  5Comments

yuany picture yuany  路  4Comments