1、安装redis模块
pip3 install redis
Looking in indexes: http://mirrors.cloud.aliyuncs.com/pypi/simple/
Collecting redisDownloading http://mirrors.cloud.aliyuncs.com/pypi/packages/bb/f1/a384c5582d9a28e4a02eb1a2c279668053dd09aafeb08d2bd4dd323fc466/redis-5.0.3-py3-none-any.whl (251 kB)|████████████████████████████████| 251 kB 1.4 MB/s
Collecting async-timeout>=4.0.3; python_full_version < "3.11.3"Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/a7/fa/e01228c2938de91d47b307831c62ab9e4001e747789d0b05baf779a6488c/async_timeout-4.0.3-py3-none-any.whl (5.7 kB)
Installing collected packages: async-timeout, redis
Successfully installed async-timeout-4.0.3 redis-5.0.3
2、编写python脚本
import redis# 连接到 Redis
r = redis.Redis(host='redis地址', port=6379, db=3, password='密码')# 定义要匹配的键的前缀
prefix = 'kube:mark:history_data_swap_'# 初始化游标
cursor = '0'# 循环迭代匹配的键并删除它们
while True:# 使用 SCAN 命令迭代匹配的键cursor, keys = r.scan(cursor, match=prefix + '*', count=1000)# 删除匹配的键for key in keys:r.delete(key)# 如果游标为 0,则说明已经迭代完所有键if cursor == b'0':breakprint("删除完成")
3、执行脚本
python3 redis-del.py