4

Now I am connect to redis, find some keys like this:

> keys "user:login:user:*"
user:login:user:62668
user:login:user:61970
user:login:user:63586

......

Now I want to delete this keys, I have tried:

keys "user:login:user:*" | xargs del

keys "user:login:user:*" | del

keys "user:login:user:*" | redis-cli xargs del

both could not work, what should I do to delete it batch for one action?

Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • See https://stackoverflow.com/questions/61366419/how-to-atomically-delete-millions-of-keys-matching-a-pattern-using-pure-redis/61366420#61366420 – LeoMurillo Dec 20 '20 at 06:44

2 Answers2

3

Better way

Although @Guy Korland answered the question, but that way may cause long-term blocking. We should always remmber redis use single thread to operate data, so if you have a lot of keys match user:login:user:* or each key's type is list, set, hash, sorted_set with a lot of elements. The deletion process will cost lots of time, redis cannot respond to other command.

redis-cli --scan --pattern users:* | xargs redis-cli unlink

use scan && unlink instead of keys && del can avoid blocking.

For the difference between scan and keys, unlink and del:

scan vs keys
del vs unlink

spike 王建
  • 1,556
  • 5
  • 14
1

You need to run redis-cli with --raw to get the result clean as arguments fro the redis-cli del

redis-cli del `redis-cli --raw keys "user:login:user:*"`
Guy Korland
  • 9,139
  • 14
  • 59
  • 106