批量删除redis中的数据
使用shell命令中的xargs实现redis批量keys操作 redis-cli
Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]
-h <hostname> Server hostname (default: 127.0.0.1).
-p <port> Server port (default: 6379).
-s <socket> Server socket (overrides hostname and port).
-a <password> Password to use when connecting to the server.
-r <repeat> Execute specified command N times.
-i <interval> When -r is used, waits <interval> seconds per command.
It is possible to specify sub-second times like -i 0.1.
-n <db> Database number.
-x Read last argument from STDIN.
-d <delimiter> Multi-bulk delimiter in for raw formatting (default:
).
-c Enable cluster mode (follow -ASK and -MOVED redirections).
--scan List all keys using the SCAN command.
--pattern <pat> Useful with --scan to specify a SCAN pattern.
单机版删除方式
redis-cli 2>/dev/null -a pwd -n 2 keys user*|xargs redis-cli 2>/dev/null -a pwd -n 2 del
命令描述,部分数据需根据自身替换:
2>/dev/null:侧面忽略redis-cli输出的警告 -a pwd:认证,需填入redis 密钥 -n 2:指定库 “2” ,相当于 select 2 keys ‘user*’:找到前缀为 “user” 的数据 |xargs 等等:删除上文找到的前缀为 “user” 的数据 不存在key前缀为 "user"的数据时,执行报error
集群版删除方式
redis-cli 2>/dev/null -a password -n 0 -p 6379 keys "TSP:CPSP*" | xargs -i redis-cli 2>/dev/null -a password -n 0 -p 6379 del {
}
