Delete array of keys in redis using node-redis

abhaygarg12493 picture abhaygarg12493 · Sep 29, 2015 · Viewed 15.8k times · Source

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"

Answer

Philip O'Brien picture Philip O'Brien · Sep 29, 2015

You can just pass the array as follows

var redis = require("redis"),
    client = redis.createClient();

client.on("error", function (err) {
    console.log("Error " + err);
});

client.set("aaa", "aaa");
client.set("bbb", "bbb");
client.set("ccc", "ccc");

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


client.keys("*", function (err, keys) {
    keys.forEach(function (key, pos) {
         console.log(key);
    });
});

client.del(keys, function(err, o) {
});

client.keys("*", function (err, keys) {
    keys.forEach(function (key, pos) {
         console.log(key);
    });
});

If you run the above code you will get the following output

$ node index.js
string key
hash key
aaa
ccc
bbb
string key
hash key

showing the keys printed after being set, but not printed after deletion