通常给出的方法如下

./redis-cli -a password keys "test*" | xargs ./redis-cli -a password del

但这是在单机情况下可以删除,如果是在Redis集群中会报错;

实验如下:

造数:

for x in {1..100};do echo $x;redis-cli -h 127.0.0.1 -p 6379 set k$x v$x;done
 ~]# redis-cli -h 127.0.0.1 -p 6379 keys "k*"   
 1) "k52"
...
37) "k75"
38) "k3"
39) "k79"

 删除

~]# redis-cli -h 127.0.0.1 -p 6379 keys "k*" | xargs redis-cli -h 127.0.0.1 -p 6379 del  
(error) CROSSSLOT Keys in request don't hash to the same slot

我们可以写个for循环来删;

for x in `redis-cli -h 127.0.0.1 -p 6379 keys "k*"`;do echo $x; redis-cli -h 127.0.0.1 -p 6379 del $x;done     
k52
(integer) 1
k6
(integer) 1
.
.
.
(integer) 1
k79
(integer) 1