1、暴力删除
使用keys * 扫描所有的key,然后批量删除。key较多时,会阻塞redis,生产环境中需要慎重,适合并发小,keys数量少的场景。
关键命令:
docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 keys "a*" |xargs redis-cli -h 172.17.0.17 -p 6379 -n 0 del"
[root@VM-0-17-centos ~]#
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 set a1 1"
OK
[root@VM-0-17-centos ~]#
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 set a2 2"
OK
[root@VM-0-17-centos ~]#
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 set a3 3"
OK
[root@VM-0-17-centos ~]#
[root@VM-0-17-centos ~]#
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 keys "a*""
1) "a1"
2) "a2"
3) "a3"
[root@VM-0-17-centos ~]#
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 keys "a*" |xargs redis-cli -h 172.17.0.17 -p 6379 -n 0 del"
(integer) 3
[root@VM-0-17-centos ~]#
[root@VM-0-17-centos ~]#
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 keys "a*""
(empty array)
###如果需要密码,可以加上-a参数
2、优雅删除
关键命令:
docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 --scan --pattern "a*" |xargs -n 1000 -r redis-cli -h 172.17.0.17 -p 6379 -n 0 del"
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 set a1 1"
OK
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 set a2 2"
OK
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 set a3 3"
OK
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 --scan --pattern "a*""
a2
a1
a3
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 --scan --pattern "a*" |xargs -N 1000 redis-cli -h 172.17.0.17 -p 6379 -n 0 del"
xargs: unrecognized option: N
BusyBox v1.31.1 () multi-call binary.
Usage: xargs [OPTIONS] [PROG ARGS]
Run PROG on every item given by stdin
-0 Input is separated by NULs
-a FILE Read from FILE instead of stdin
-r Don't run command if input is empty
-t Print the command on stderr before execution
-p Ask user whether to run each command
-E STR,-e[STR] STR stops input processing
-I STR Replace STR within PROG ARGS with input line
-n N Pass no more than N args to PROG
-s N Pass command line of no more than N bytes
-P N Run up to N PROGs in parallel
-x Exit if size is exceeded
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 --scan --pattern "a*" |xargs -n 1000 -r redis-cli -h 172.17.0.17 -p 6379 -n 0 del"
(integer) 3
[root@VM-0-17-centos ~]#
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 --scan --pattern "a*""
[root@VM-0-17-centos ~]#
###如果需要密码,可以加上-a参数
备注:文章来源:https://www.toymoban.com/news/detail-518322.html
xargs -n 参数的作用,表示每次执行的参数数量文章来源地址https://www.toymoban.com/news/detail-518322.html
### 注意-n参数,可以每次删除适量的
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 set a1 3"
OK
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 set a2 3"
OK
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 set a3 3"
OK
[root@VM-0-17-centos ~]# docker run --rm --name redis-cli -it goodsmileduck/redis-cli sh -c "redis-cli -h 172.17.0.17 -p 6379 -n 0 --scan --pattern "a*" |xargs -n 1 -r redis-cli -h 172.17.0.17 -p 6379 -n 0 del"
(integer) 1
(integer) 1
(integer) 1
到了这里,关于优雅删除Redis中以xx开头的key的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!