redis批量删除key
1.生成key
192.168.1.50:6379> set name1 hxl
OK
192.168.1.50:6379> set name2 hxl02
OK
192.168.1.50:6379> set name3 hxl03
OK
192.168.1.50:6379> get name1
"hxl"
192.168.1.50:6379> get name2
"hxl02"
192.168.1.50:6379> get name3
"hxl03"
192.168.1.50:6379> exit
批量删除
[root@cdc-henan-cdhworker02 /]# /usr/local/redis/bin/redis-cli -h 192.168.1.50 -p 6379 -a test123 KEYS "*name*"|xargs /usr/local/redis/bin/redis-cli -h 192.168.1.50 -a test123 del
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3
检查是否已经删除
[root@cdc-henan-cdhworker02 /]# /usr/local/redis/bin/redis-cli -h 192.168.1.50 -p 6379 -a test123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.50:6379> get name1
(nil)
192.168.1.50:6379> get name2
(nil)
192.168.1.50:6379> get name3
(nil)
192.168.1.50:6379>
2.批量删除(方法1)
2.1 导出key到文件
/usr/local/services/redis/bin/redis-cli -h 192.168.1.100 -p 6379 -a testtest KEYS "*STOCK*">/tmp/rediskey.txt
若对这些key进行其他的操作,比如del,可以在每行前面加上del,使用vi命令如下:
:%s/^/DEL /
或是用sed实现
sed 's/^/DEL &/g' a.txt
这个时候生成的文件内容就是redis命令了,如下:
DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:3538
DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:3157
DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:23747
DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:24663
DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:15391
DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:24059
...
2.2 不采用管道方式:
cat /tmp/hospitalId_last.txt | /usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456
采用管道方式:
cat /tmp/hospitalId_last.txt | /usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456 --pipe
采用管道的方式需要将文件进行格式转换
[root@master ~]#yum install unix2dos
[root@master ~]#unix2dos hospitalId_last.txt
-- The End --
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?