优雅删除Redis中以xx开头的key
优雅删除Redis中以xxx开头的key
·
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参数
备注:
xargs -n 参数的作用,表示每次执行的参数数量
### 注意-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
更多推荐
已为社区贡献1条内容
所有评论(0)