I have a list of keys, and would like to delete all of them. No pattern matching, nothing, just simple delete. I don't want to run a loop, as there will be around 3-4k keys.
I was trying to pass a list into the delete function, but it didn't work
redis_keys = [key1,key2,key3,key4....keyn]
redis.delete(redis_keys)
In the docs it shows
but not how to pass multiple keys. On SO too all questions are related to deleting while matching keys with pattern, but not with exact keys available.
We can use keys command like below to delete all the keys which match the given patters “ user*" from the Redis. Note :- Not recommended on production Redis instance. I have written Lua script to delete multiple keys by pattern . Script uses the scan command to delete the Redis cache key in incremental iteration.
Redis does not offer a way to bulk delete keys. You can however use redis-cli and a little bit of command line magic to bulk delete keys without blocking redis. This command will delete all keys matching users:* If you are in redis 4.0 or above, you can use the unlink command instead to delete keys in the background.
flushdb() # Delete all keys of currently selected database instance. r. flushall() # Delete all keys of entire database.
The *names
syntax means that you can pass multiple variables via
redis.delete(*redis_keys)
which is really just a shorthand notation for
redis.delete(redis_keys[0], redis_keys[1], redis_keys[2], ..., redis_keys[-1])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With