笔记 - 使用python脚本模糊删除redis数据(redis scan)

130 阅读1分钟

环境

  • python3
  • Linux

安装redis库

python3 -m pip install redis

脚本

from redis import StrictRedis

if __name__ == "__main__":
    try:
        conn = StrictRedis(host='localhost', port=6379, password="xxx")
        keys = conn.scan_iter()
        count = 0
        for key in keys:
            key = key.decode("utf-8")
            if "server-name-xxx:key-desc" in key:
                print(key)
                print(conn.delete(key))
                count += 1
        print(count)
    except Exception as e:
        print(e)