Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
One liner for deleting based on a pattern in redis. KEYS supports wildcards, delete doesn't. No worries xargs to the rescue. You might not need HOST, or PORT depending on your setup. You might need to sudo BOTH commands depending on your setup.
redis-cli -h <HOST> -p <PORT> KEYS "<PATTERN>" | xargs -i% redis-cli -h <HOST> -p <PORT> DEL %
@NotoriousPyro

This comment has been minimized.

Copy link

NotoriousPyro commented Nov 14, 2018

Using keys is dangerous in a production environment. Therefore in development, you should get out of the habit of using it. This works better (and can be varied):

batchSize=1000; host="127.0.0.1"; port=6379; pattern='my:pattern'; redis-cli -h $host -p $port --scan --pattern $pattern | xargs -L $batchSize redis-cli -h $host -p $port DEL
@yellow1912

This comment has been minimized.

Copy link

yellow1912 commented Nov 14, 2018

this has issue with keys containing special characters, you need to wrap each key in single quote i think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.