Search code examples
redisredis-cli

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:

  • myprefix:abc
  • myprefix:def
  • myprefix:ghi

Not deleted:

  • myprefix:jkl mno
  • myprefix:pqr stu
  • myprefix:vwx yza

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


Solution

  • 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