Skip to content

Instantly share code, notes, and snippets.

@cmbankester
Created May 8, 2015 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmbankester/6eaa6588f030fc9ae5ea to your computer and use it in GitHub Desktop.
Save cmbankester/6eaa6588f030fc9ae5ea to your computer and use it in GitHub Desktop.
Redis Scan-Remove
function scanRemove(cursor, prefix, count) {
return redis
.scan(cursor, 'MATCH', `${prefix}*`, 'COUNT', count)
.then(([cur, keys]) => redis.del(keys).then(() => Promise.resolve(cur)))
.then(cur => (cur !== 0 && cur !== '0') ? scanRemove(cur, prefix, count) : Promise.resolve());
}
function removeAllMatching(prefix, count=10) {
return scanRemove(0, prefix, count)
.catch(err => {throw new Error(`Scan-remove failed: ${err.message}`);});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment