60

Is there any way to remove/delete an entry by key, using Node_redis? I can't see any such option from the docs..

UpTheCreek
  • 31,444
  • 34
  • 152
  • 221

7 Answers7

135

You can del use like this:

redis.del('SampleKey');
jazgot
  • 1,943
  • 14
  • 25
himanshu yadav
  • 1,818
  • 2
  • 13
  • 5
  • 8
    This is the actual answer, giving a link to the redis docs was not an answer, the question was tagged with `node-redis` – Sandwich Oct 26 '17 at 11:18
53

Here you can see what redis commands are work in this library node_redis github

As you can see "del" command is in the list.

And this command allow you delete keys from selected db as Jonatan answered.

Jackson Egan
  • 2,715
  • 4
  • 18
  • 26
Elephant
  • 1,346
  • 10
  • 11
23

As everyone above has stated you can use del function. You can assure the successful delete operation using this syntax.

client.del('dummyvalue', function(err, response) {
   if (response == 1) {
      console.log("Deleted Successfully!")
   } else{
    console.log("Cannot delete")
   }
})

Because the DEL command will return (integer) 1 in successful operation.

    redis 127.0.0.1:6379> DEL key 
    Success: (integer) 1
    Unsuccess: (integer) 0
Tolsee
  • 1,695
  • 14
  • 23
13

If I remember things correctly, del should do it.

Jonatan Hedborg
  • 4,382
  • 21
  • 30
9

Hope this will help you

let redis = require("redis");

var redisclient = redis.createClient({
  host: "localhost",
  port: 6379
});

redisclient.on("connect", function () {
  console.log("Redis Connected");
});

redisclient.on('ready', function () {
  console.log("Redis Ready");
});

redisclient.set("framework", "AngularJS", function (err, reply) {
  console.log("Redis Set" , reply);
});

redisclient.get("framework", function (err, reply) {
  console.log("Redis Get", reply);
});

redisclient.del("framework",function (err, reply) {
  console.log("Redis Del", reply);
});
Renish Gotecha
  • 2,232
  • 22
  • 21
0

Suppose we want to delete key="randomkey":

redisClient.del(key, function(err, reply) {
  if (err)
    throw err;

  console.log("deleteRedisKey",reply)
  resolve(reply);
});
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
-10

Using sailsJs you can do this :

let id_todel = "5b845f9ea7f954bb732fdc52";
await sails.getDatastore('redis').leaseConnection(function during(db, proceed) {
      db.del(id_todel );
      return proceed(undefined, undefined);
});
Zakaria.dem
  • 293
  • 5
  • 10