I came across this issue the other day, looking at our redis logs some keys had been defined with backslashes in. This Isn't advisable, but it had happened, and we needed to get rid of them.
If you manually use redis-cli DEL
you can delete each key, but if you have a few thousand that isn't super helpful. All of the docs and posts online refer to the standard way of deleting a key as:
redis-cli KEYS TESTING* | xargs redis-cli DEL
This works great, but if your key has a backslashes in, they get escaped out when piped to xargs, so they won't be deleted. To get around this you need to first pipe to sed to add the extra slashes sed 's/\(\\\)/\\\1/g'
, before piping to the delete command.
~ > redis-cli SET "TESTING:This\String\Has\Backslashes" "Try delete me"
OK
~ > redis-cli KEYS TESTING* | xargs echo
TESTING:ThisStringHasBackslashes
~ > redis-cli KEYS TESTING* | xargs redis-cli DEL
(integer) 0
~ > redis-cli KEYS TESTING*
1) "TESTING:This\\String\\Has\\Backslashes"
~ > redis-cli KEYS TESTING* | sed 's/\(\\\)/\\\1/g' | xargs redis-cli DEL
(integer) 1
~ > redis-cli KEYS TESTING*
(empty list or set)
~ >
Alternatively instead of using sed
then xargs
you can use parallel
, although some hosts may not have that installed.
~ > redis-cli SET "TESTING:This\String\Has\Backslashes" "Try delete me"
OK
~ > redis-cli KEYS TESTING* | parallel redis-cli DEL
1
~ > redis-cli KEYS TESTING*
(empty list or set)
I hope this saves someone Googling in the future some time!
:wq
BTC | 15FYeMgQsETViUcc8JtSmoHmCAmADy2Ymn
ARK | ANKmYTHV7QdkcLz2R65uMzEvcRRdz2iRQm