Reputation: 7465
How does one deletes keys with certain prefix from Redis 5+?
I've tried following, yet didn't work for me(
root@1acb94e11aa2:/data# redis-cli --version
redis-cli 5.0.4
root@1acb94e11aa2:/data# redis-cli -n 9 KEYS ISO:* | wc -l
935
root@1acb94e11aa2:/data# redis-cli -n 9 KEYS ISO:* | xargs -0 redis-cli -n 9 DEL
(integer) 0
root@1acb94e11aa2:/data# redis-cli -n 9 KEYS ISO:* | wc -l
935
root@1acb94e11aa2:/data# redis-cli -n 9 --scan --pattern ISO:* | xargs -0 redis-cli -n 9 unlink
(integer) 0
root@1acb94e11aa2:/data#
Please advise.
Upvotes: 0
Views: 721
Reputation: 49942
As long as your key names do not include spaces, you should be able to run this:
$ redis-cli -n 9 --scan --pattern "ISO:*" | xargs -n 1 redis-cli -n 9 UNLINK
EDIT: if they do include spaces, you can do:
$ redis-cli -n 9 --scan --pattern "ISO:*" | xargs -n 1 -d "\n" redis-cli -n 9 UNLINK
Upvotes: 1
Reputation: 7465
FIX:
root@1acb94e11aa2:/data# redis-cli -n 9 KEYS ISO:* | xargs -d "\n" redis-cli -n 9 del
(integer) 262
root@1acb94e11aa2:/data#
root@1acb94e11aa2:/data# redis-cli -n 9 KEYS ISO:*
(empty list or set)
root@1acb94e11aa2:/data#
Upvotes: 0