Node_redis - how to remove a key?
Asked Answered
S

7

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

Sf answered 5/3, 2013 at 8:59 Comment(0)
E
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.

Euler answered 5/3, 2013 at 9:8 Comment(3)
@Euler hdel is not there but it works. The reference should be redis.io/commands since "It supports all Redis commands" (quoted from github github.com/NodeRedis/node_redis).Kroon
@Euler nevermind, i was looking at ioredis github.com/luin/ioredis/blob/master/lib/command.jsKroon
The link provided is now 404ing on github.com.Villein
C
140

You can del use like this:

redis.del('SampleKey');
Cerenkov answered 17/7, 2014 at 9:55 Comment(1)
This is the actual answer, giving a link to the redis docs was not an answer, the question was tagged with node-redisPpm
E
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.

Euler answered 5/3, 2013 at 9:8 Comment(3)
@Euler hdel is not there but it works. The reference should be redis.io/commands since "It supports all Redis commands" (quoted from github github.com/NodeRedis/node_redis).Kroon
@Euler nevermind, i was looking at ioredis github.com/luin/ioredis/blob/master/lib/command.jsKroon
The link provided is now 404ing on github.com.Villein
W
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
Westley answered 27/3, 2017 at 9:52 Comment(0)
C
13

If I remember things correctly, del should do it.

Circlet answered 5/3, 2013 at 9:3 Comment(0)
K
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);
});
Keavy answered 16/9, 2019 at 13:1 Comment(0)
B
0

Suppose we want to delete key="randomkey":

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

  console.log("deleteRedisKey",reply)
  resolve(reply);
});
Becker answered 28/11, 2022 at 17:21 Comment(0)
H
-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);
});
Hedberg answered 10/9, 2018 at 20:39 Comment(1)
This is specific to sailsTourane

© 2022 - 2024 — McMap. All rights reserved.