Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting multiple keys in python-Redis in single command

Tags:

python

redis

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

enter image description here

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.

like image 900
dilettante_aficionado Avatar asked Oct 28 '17 12:10

dilettante_aficionado


People also ask

How do I delete multiple keys in Redis?

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.

How do I delete all keys matching a pattern in Redis?

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.

How do I clear Redis in Python?

flushdb() # Delete all keys of currently selected database instance. r. flushall() # Delete all keys of entire database.


1 Answers

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])
like image 152
jmd_dk Avatar answered Oct 25 '22 01:10

jmd_dk