Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete array of keys in redis using node-redis #873

Closed
abhaygarg opened this issue Sep 29, 2015 · 5 comments
Closed

Delete array of keys in redis using node-redis #873

abhaygarg opened this issue Sep 29, 2015 · 5 comments
Labels

Comments

@abhaygarg
Copy link

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"

@stockholmux
Copy link
Contributor

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.

@BridgeAR
Copy link
Contributor

@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).

@abhaygarg
Copy link
Author

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

@knoxcard
Copy link
Contributor

knoxcard commented Nov 23, 2018

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

@stockholmux
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants