Last active
April 29, 2025 17:46
-
-
Save filipeandre/81f0711e27887b94755a719536bd3945 to your computer and use it in GitHub Desktop.
Helper script to delete redis cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const redis = require('redis'); | |
const readline = require('readline'); | |
async function connectRedis(host, port, db, password) { | |
const client = redis.createClient({ | |
socket: { host, port }, | |
database: db, | |
password | |
}); | |
await client.connect(); | |
return client; | |
} | |
async function deleteKeys(client, keys) { | |
if (!keys.length) { | |
console.log("No keys provided to delete."); | |
return; | |
} | |
const deleted = await client.del(keys); | |
console.log(`Deleted ${deleted} keys.`); | |
} | |
async function deleteByPattern(client, pattern) { | |
const keys = await client.keys(pattern); | |
if (!keys.length) { | |
console.log(`No keys found matching pattern: ${pattern}`); | |
return; | |
} | |
const deleted = await client.del(keys); | |
console.log(`Deleted ${deleted} keys matching pattern '${pattern}'.`); | |
} | |
async function flushDatabase(client) { | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
rl.question("Are you sure you want to flush the entire Redis database? (yes/no): ", async (answer) => { | |
if (answer.toLowerCase() === 'yes') { | |
await client.flushDb(); | |
console.log("Redis database flushed."); | |
} else { | |
console.log("Aborted flushing the Redis database."); | |
} | |
rl.close(); | |
await client.disconnect(); | |
}); | |
} | |
function parseArgs() { | |
const args = process.argv.slice(2); | |
const options = {}; | |
for (let i = 0; i < args.length; i++) { | |
const arg = args[i]; | |
switch (arg) { | |
case '--host': | |
options.host = args[++i]; | |
break; | |
case '--port': | |
options.port = parseInt(args[++i], 10); | |
break; | |
case '--db': | |
options.db = parseInt(args[++i], 10); | |
break; | |
case '--password': | |
options.password = args[++i]; | |
break; | |
case '--keys': | |
options.keys = []; | |
while (args[i + 1] && !args[i + 1].startsWith('--')) { | |
options.keys.push(args[++i]); | |
} | |
break; | |
case '--pattern': | |
options.pattern = args[++i]; | |
break; | |
case '--flush': | |
options.flush = true; | |
break; | |
default: | |
console.error(`Unknown option: ${arg}`); | |
process.exit(1); | |
} | |
} | |
options.host = options.host || 'localhost'; | |
options.port = options.port || 6379; | |
options.db = options.db || 0; | |
return options; | |
} | |
async function main() { | |
const options = parseArgs(); | |
const client = await connectRedis(options.host, options.port, options.db, options.password); | |
if (options.keys) { | |
await deleteKeys(client, options.keys); | |
await client.disconnect(); | |
} else if (options.pattern) { | |
await deleteByPattern(client, options.pattern); | |
await client.disconnect(); | |
} else if (options.flush) { | |
await flushDatabase(client); | |
} else { | |
console.log("No action specified."); | |
await client.disconnect(); | |
process.exit(1); | |
} | |
} | |
main().catch(err => { | |
console.error(err); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Delete all:
node redis.js --host $CACHE_HOST --flush