UpTheCreek
UpTheCreek

Reputation: 32391

Node_redis - how to remove a key?

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

Upvotes: 61

Views: 99378

Answers (6)

Vishvas Jaiswal
Vishvas Jaiswal

Reputation: 1

Suppose we want to delete key="randomkey":

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

  console.log("deleteRedisKey",reply)
  resolve(reply);
});

Upvotes: 0

Renish Gotecha
Renish Gotecha

Reputation: 2522

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);
});

Upvotes: 9

Tolsee
Tolsee

Reputation: 1705

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

Upvotes: 23

Elephant
Elephant

Reputation: 1344

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.

Upvotes: 52

himanshu yadav
himanshu yadav

Reputation: 1908

You can del use like this:

redis.del('SampleKey');

Upvotes: 142

Jonatan Hedborg
Jonatan Hedborg

Reputation: 4432

If I remember things correctly, del should do it.

Upvotes: 13

Related Questions