Skip to content

Instantly share code, notes, and snippets.

@mattweyant
Created June 4, 2012 19:05
Show Gist options
  • Save mattweyant/2870203 to your computer and use it in GitHub Desktop.
Save mattweyant/2870203 to your computer and use it in GitHub Desktop.
Delete all keys from Redis matching a regular expression
# All credit: http://stackoverflow.com/questions/4006324/how-to-atomically-delete-keys-matching-a-pattern-with-redis
redis-cli [options] KEYS "prefix:*" | xargs redis-cli [options] DEL
@ptz0n
Copy link

ptz0n commented Jul 18, 2013

Sorry but glob is not regular expressions. see http://redis.io/commands/keys for available pattern matching. Or pipe it via a regular expression with grep:

redis-cli keys "user:*" | grep "user:[0-9]\+$" | xargs redis-cli DEL

@lagden
Copy link

lagden commented Aug 9, 2017

@ptzOn
Could that cause redis blocking?

@VikramMoule
Copy link

@lagden
Yes. Using KEYS is a blocking operation. You should replace it with SCAN.
The SO post has also been updated.
$ redis-cli --scan --pattern ":foo:bar:" | xargs -L 100 redis-cli DEL

@sandcastle
Copy link

sandcastle commented Oct 9, 2018

DEL is also blocking, use UNLINK for non-blocking removal

@jessecurry
Copy link

If your keys have any escape characters you need to change the xargs delimiter:

redis-cli --scan --pattern "horizon:failed:*" | xargs -n 1 -d '\n' redis-cli DEL

@peterpoliwoda
Copy link

How can you do this with authentication?
I need to login to my remote redis first

redis-cli -u redis://admin@remote.url.to.redis:portNumber 

to get into the redis-cli and run auth with a password to get into the db:

remote.url.to.redis:portNumber> auth MYPASSWORD
OK

Is it possible then to run the KEYS patter*n command and pipe it through to a DEL inside of redis-cli already?
Something along the lines of

remote.url.to.redis:portNumber> KEYS patter*n | DEL *

This above doesn't work, I'm looking for something that might.

@Ali-Mahmood
Copy link

Ali-Mahmood commented Apr 17, 2020

@peterpoliwoda you can do the following:

redis-cli -h <HOST> -p <PORT> -a <PASSWORD> --scan --pattern "patter*n" | xargs redis-cli -h <HOST> -p <PORT> -a <PASSWORD> unlink

Using unlink is better then using del as stated in: https://redis.io/commands/unlink

@carltondickson
Copy link

Thanks @ali
These commands are ridiculously slow when you have millions of keys

@prempopatia
Copy link

@peterpoliwoda you can do the following:

redis-cli -h <HOST> -p <PORT> -a <PASSWORD> --scan --pattern "patter*n" | xargs redis-cli -h <HOST> -p <PORT> -a <PASSWORD> unlink

Using unlink is better then using del as stated in: https://redis.io/commands/unlink

Thanks, it did work.

@mdutkin
Copy link

mdutkin commented Jan 20, 2022

If your keys have any escape characters you need to change the xargs delimiter:

redis-cli --scan --pattern "horizon:failed:*" | xargs -n 1 -d '\n' redis-cli DEL

that's my case, it worked - thanks a lot @jessecurry

@anjanesh
Copy link

How do I achieve the same using the redis pypi package ? Right now I'm using r.delete(*r.keys("someKey_*"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment