Redis: How to Use the command to delete keys in batches

In redis, there is no command to delete keys directly according to regular expressions, only the Del key1 command

However, redis has a command to obtain keys through regular expressions: keys “regular expressions”

You can delete keys in batches with the help of xargs command, and pass the found key value to del as a parameter

redis-cli keys “xxxx*” | xargs del

Full command

redis-cli -h 192.169.1.71 -p 7001 -a 123456 keys xxxx* | xargs -r -t -n1 redis-cli -h 192.169.1.71 -p 7001 -a 123456 del

[note]

The xargs command needs to be followed by the parameter – R, otherwise when the number of keys is 0, an error will be reported err wrong number of arguments for ‘del’ command

The parameter – N1 should be added after the xargs command. Otherwise, when the number of cluster keys is greater than 1, an error may be reported: crosslot keys in request don’t hash to the same slot

It is OK not to add – T. adding – t will output the content deleted each time. If not, the deleted content will not be output, but the number of keys deleted each time will still be output

Similar Posts: