Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Redis – Delete all but one key

we are currently using this command from shell to purge our Redis DB:

redis-cli -h zapi.data.com flushdb

However, now I am being told that I have to delete all keys, except for the key that is "zrtt_industry". How can I do a regular expression that will delete all but the key matching my pattern, that pattern being "zrtt_industry".

Many thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Unfortunately, Redis does not provide a native “negative match” pattern or a built-in command to flush all keys except for a specific one. You must instead retrieve all keys and then filter them out before deleting.

Approach using a shell pipeline:
1. List all keys with redis-cli keys "*".
2. Use grep -v to exclude the key zrtt_industry.
3. Pass the remaining keys to redis-cli del via xargs.

For example:

redis-cli -h zapi.data.com KEYS "*" | grep -v ‘^zrtt_industry$’ | xargs -n 1 redis-cli -h zapi.data.com DEL

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading