Search code examples
javascriptnode.jscachingredis

Redis client doesn't delete anything


I'm using node.js redis client. Before I'm running unit tests I want to clear all redis keys. For some weird reasons my code does nothing.

const redis = require('redis');

const client = redis.createClient({
  host: conf.host,
  port: conf.port,
  prefix: conf.prefix
});

client.on('connect', err => {
  // everything is OK here. I can set and get.
});

module.exports = redis;

Before tests are started I want to clear my cache:

const redis = require('file-above');

before(done => {
  redis.keys('*', function (err, rows) {
    rows.forEach(row => {
      console.log(row); // perfectly matched
      redis.del(row);
      // nothing changed. I can still get any of the rows above here
    });
  });
  done();
});

Nothing is deleted after this. I tried to add setTimeout, tried to use direct redis.del('*', done) without keys matching.

I can not find anything related to .del in redis package readme to figure out what am I doing wrong.


Solution

  • You should use flushall if appropriate. See more documentation here http://redis.io/commands/FLUSHALL. In regards to your implementation though, I think you need to pass a callback to del.