优雅删除Redis中以xx开头的key

本文介绍两种Redis键批量删除的方法:暴力删除和优雅删除。暴力删除直接扫描并删除所有匹配的键,适用于并发小、键数量不多的情况;优雅删除通过逐批删除减少对Redis的影响,适合键数量大的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值