Skip to content

Instantly share code, notes, and snippets.

@cliftonc
Created May 10, 2012 13:27
Show Gist options
  • Save cliftonc/2652981 to your computer and use it in GitHub Desktop.
Save cliftonc/2652981 to your computer and use it in GitHub Desktop.
Delete lots of redis keys
var smartDelete = function(clientName, queryKey, cb) {
var client = redis.getClient(clientName), noneRemaining = false, numDeleted = 0, lastK, duration;
async.whilst(
function () { return !noneRemaining; },
function (callback) {
client.spop(queryKey, function(err, key) {
if(key) {
client.del(key,function(err) {
numDeleted ++;
if(numDeleted === 1) {
lastK = new Date();
}
if(numDeleted % 100000 === 0) {
duration = new Date() - lastK;
lastK = new Date();
logger.silly(clientName + ' deleted ' + numDeleted + ' @ ' + (duration/100) + '/s');
}
callback(err);
});
} else {
noneRemaining = true;
callback();
}
})
},
function (err) {
logger.silly('Deleted from ' + clientName + ': ' + numDeleted);
cb(err);
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment