Delete array of keys in redis using node-redis
Asked Answered
G

4

12

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"

Gules answered 29/9, 2015 at 11:27 Comment(2)
Did you manage to solve your problem?Subsistent
actually there is problem in passing array otherwise this code works fineGules
S
16

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

Selfabsorbed answered 29/9, 2015 at 11:52 Comment(0)
G
8

Certainly at current version of node_redis (v2.6.5) it is possible to delete both with a comma separated list of keys or with an array of keys. See tests for both here.

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

client.set('foo', 'foo');
client.set('apple', 'apple');

// Then either

client.del('foo', 'apple');

// Or

client.del(['foo', 'apple']);
Geometric answered 6/2, 2017 at 14:32 Comment(3)
You say it's possible without showing how it can be done.Derbent
The link I shared shows in the tests how it can be done. However I will update my answer to show this explicitly.Geometric
It's recommended to quote the relevant part of an external link (as you've now done) in case the link is ever inaccessible.Derbent
H
3

del function is implemented directly as in Redis DB client, I.e. redis.del("aaa","bbb","ccc") will remove multiple items

To make it work with array use JavaScript apply approach:

redis.del.apply(redis, ["aaa","bbb","ccc"])
Hyperthermia answered 24/5, 2016 at 10:56 Comment(0)
Z
1

node-redis doesn't work like that but if you really have a lot of del commands it will pipeline them automatically so it is probably more efficient than you think to do it in a loop.

You can also try this module with multi:

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

client.multi([
    ["del", "key1"],
    ["del", "key2"]
]).exec(function (err, replies) {
    console.log(replies);
});
Zischke answered 29/9, 2015 at 11:44 Comment(1)
Instinctively this looks good. Do you know looping through an array of keys and adding them to a multi array to execute is more performant that simply calling .del on the array of keys itself?Rhyme

© 2022 - 2024 — McMap. All rights reserved.