redis如何批量删除key

env

  • redis4.x

steps

# 删除单个
del key

# 删除多个(Linux下的管道符批量操作)
redis-cli -h ip -a pass(密码)  keys 关键字 | xargs redis-cli -h ip -a pass(密码) del         

# 在redis的客户端连接处登陆删除
./redis-cli -h 10.111.0.xx  -a xx  keys '*str_ account*' | xargs ./redis-cli -h 10.111.0.xx  -a xx  del

# 环境变量直接使用 
ln -s  /tmp/redis/src/redis-cli  /usr/bin/
redis-cli -h 10.111.0.xx -a xx  keys '*wxsmrzResult*' | xargs redis-cli -h 10.111.0.xx  -a xx  del   

#redis db

#在没有指令限制的情况下,可以使用Redis的flushdb和flushall命令

删除当前数据库中的所有Key 
flushdb

删除所有数据库中的key       
flushall


# 要访问Redis中特定的数据库
指定数据序号为0,即默认数据库 redis-cli -n 0 keys "*" | xargs redis-cli -n 0 del

# keys指令可以进行模糊匹配,但如果 Ke 含空格,就匹配不到了
h?llo matches hello, hallo and hxllo           #? 匹配任意单个字符
h*llo matches hllo and heeeello               #* 匹配任意字符
h[ae]llo matches hello and hallo, but not hillo  # 或者
h[^e]llo matches hallo, hbllo, ... but not hello   #排除
h[a-b]llo matches hallo and hbllo             #
Use \ to escape special characters if you want to match them verbatim.