There is no command to directly delete keys based on regular expressions in redis, only del key1 key2... command
But there is a command to obtain keys through regular expressions in redis: keys "regular expression"
You can use the xargs command to delete keys in batches, and pass the found key value as a parameter to del
redis-cli keys "mailspec*" | xargs del
Complete Command:
[root@localhost redis7001]# redis-cli -h 192.169.1.71 -p 7001 -a 123456 keys em* | xargs -r -t -n1 ./redis-cli -h 192.169.1.71 -p 7001 -a 123456 del
Supplement 1: The parameter -r must be added after the xargs command, otherwise when the number of keys is 0, an error will be reported (error) ERR wrong number of arguments for &lsquo ;del’ command
Supplement 2: The parameter -n1 must be added after the xargs command, otherwise when the number of keys in the cluster is greater than 1, an error may be reported (error) CROSSSLOT Keys in request don’t hash to the same slot
Additional 3: You can also add -t without adding -t. Adding -t will output the deleted content each time. If you do not add -t, the deleted content will not be output. , but the number of keys deleted each time will still be output.
1. To delete all keys, you can use redis’ own command :
flushdb Delete all keys in the current database
flushall Delete keys in all databases
2. Use xargs in linux to delete all keys
redis-cli keys “*” | xargs redis-cli del
3. Delete keys containing certain keywords
redis-cli keys “xxx*” | xargs redis- cli del
4. If you need to specify a password, you can use it like this:
redis-cli -a pwd keys “*” | xargs redis-cli -a pwd del
The above is the detailed content of How to use the command to delete keys in batches in Redis. For more information, please follow other related articles on the PHP Chinese website!