Redis Python - How to Delete All Keys According to a Specific Pattern in Python, Without Python Iterating

Redis Python - how to delete all keys according to a specific pattern In python, without python iterating

I think the

 for key in x: cache.delete(key)

is pretty good and concise. delete really wants one key at a time, so you have to loop.

Otherwise, this previous question and answer points you to a lua-based solution.

How to remove all elements in Redis

You're storing in Redis an object (dict_) under the key called 'pythonDict'. Just call cli.delete("pythonDict") to delete it.

How to delete all keys in Redis matching pattern from within redis-cli repl?

Run this command inside redis-cli :

EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 prefix:*

Replace prefix:* with your required pattern. The output will be the number of keys deleted.

Deleting multiple keys in python-Redis in single command

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])

How to atomically delete keys matching a pattern using Redis

Starting with redis 2.6.0, you can run lua scripts, which execute atomically. I have never written one, but I think it would look something like this

EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 prefix:[YOUR_PREFIX e.g delete_me_*]

Warning: As the Redis document says, because of performance maters, keys
command should not use for regular operations in production, this
command is intended for debugging and special operations. read
more

See the EVAL documentation.



Related Topics



Leave a reply



Submit