XCEPTION
XCEPTION

Reputation: 1753

Delete keys with spaces

I am trying to delete multiple keys from my remote Redis db using the following command.

redis-cli -h <host> -p 6379 KEYS "myprefix:*" | xargs redis-cli -h <host> -p 6379 DEL

It has deleted all the matching keys except the ones having spaces in them.

For e.g.

Deleted:

Not deleted:

What should be my query pattern so that these get deleted too? I tried googling but was unable to find any solution.

Upvotes: 6

Views: 2289

Answers (3)

pratpor
pratpor

Reputation: 2104

Here is an another hack to make it work on Mac.

redis-cli KEYS 'pattern*' | xargs -0 -n1 'echo' | sed "s/^/'/g" | sed "s/$/'/g" | xargs redis-cli DEL

This basically puts quote around your keys before passing to redis-cli DEL

Upvotes: 0

uosjead
uosjead

Reputation: 436

For anyone using Mac OS xargs (BSD) instead of linux (GNU) xargs the following works

redis-cli -h <host> -p 6379 keys "myprefix:*" | xargs -I% redis-cli -h <host> -p 6379 del %

or for other weird characters like speech marks this works

redis-cli -h <host> -p 6379 --scan --pattern "myprefix:*" | tr '\n' '\0' | xargs -0 -I% redis-cli -h <host> -p 6379 del %

Upvotes: 1

cbp
cbp

Reputation: 25628

The issue is with xargs, not your KEYS query. If you run your query, you will notice that it correctly returns the keys with spaces in them. The problem is that by default xargs splits the strings piped into it by blanks and newlines. To change this behaviour so that it only delimits by newlines, add the -d "\n" option. For example:

redis-cli -h <host> -p 6379 keys "myprefix:*" | xargs -d "\n" redis-cli -h <host> -p 6379 del

Upvotes: 10

Related Questions