redis 使用scan 批量删除指定前缀的key

1、插入测试数据

--测试插入数据
[root@test01 ~]# vim /home/redis_insert_date.py
#!/usr/local/bin/python3
# coding=utf-8
import redis
import sys
import datetime

def create_testdata():
	r = redis.StrictRedis(host='172.31.0.xx', port=6379, password='xxx', db=0)
	counter = 0
	with r.pipeline(transaction=False) as p:
		for i in range(0, 1000):
			p.set('key:' + str(i), "value" + str(i))
			counter = counter + 1
			if (counter == 100):
				p.execute()
				counter = 0
				print("set by pipline loop")

if __name__ == "__main__":
	create_testdata()
[root@test01 ~]#
[root@test01 ~]# chmod +x /home/redis_insert_date.py
[root@test01 ~]# /home/redis_insert_date.py

2、使用scan命令批量删除指定前缀的数据 

--删除指定前缀的key 并且每次删除10个,但是不能控制删除的行数,直到删除掉所有满足条件的所有key
[root@LG-wiki-96 ~]# redis-cli -h 172.31.0.xx -p 6379 -a xxx -n 1 --scan --pattern "key:*" | xargs -L 10 redis-cli -h 172.31.0.xx -p 6379 -a xxx -n 0 del
Warning: Using a password with '-a' option on the command line interface may not be safe.
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 10
Warning: Using a password with '-a' option on the command line interface may not be safe.
(integer) 1

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值