方法1(推荐)
用conn.delete删除hash表名。
conn = RedisCluster(startup_nodes=nodes,decode_responses=True, cluster_down_retry_attempts=20)
conn.hset('测试', 'test1', '1')
conn.hset('测试', 'test2', '2')
print(conn.hgetall('测试'))
conn.delete('测试')
print(conn.hgetall('测试'))
输出:
{'test1': '1', 'test2': '2'}
{}
方法2
遍历hash表的键值对,用conn.hdel删除键。
conn = RedisCluster(startup_nodes=nodes,decode_responses=True, cluster_down_retry_attempts=20)
conn.hset('测试', 'test1', '1')
conn.hset('测试', 'test2', '2')
print(testkv:=conn.hgetall('测试'))
for k in testkv:
conn.hdel('测试',k)
print(conn.hgetall('测试'))
输出:
{'test1': '1', 'test2': '2'}
{}