Deleting multiple keys in python-Redis in single command
Asked Answered
D

2

11

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.

Dennisedennison answered 28/10, 2017 at 12:9 Comment(0)
A
26

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])
Adley answered 28/10, 2017 at 12:12 Comment(0)
K
0

If you know the keys you want to delete, you can combine your lookup with your delete via something like this:

my_key = 'items.per.day.*'
redis.delete(*redis.keys(my_key))
Kiangsu answered 1/5, 2023 at 19:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.