How to delete keys for which the value matches a pattern in Redis?
Solution
$ redis-cli --scan --pattern "car:vw*" | xargs redis-cli del
Explanation
redis-cli
– the Redis command line interface. It has an interactive REPL (Read Eval Print Loop) mode where the user types Redis commands and received replies, and a command mode where redis-cli
is executed with additional arguments and the reply is printed to the standard output.
We use it in command mode to run the --scan
command with the --pattern
option that prints all the key names that match de specified pattern “car:vw*”.
Then, using xargs
we use the output of the --scan
command as input for the del
command that removes the specified keys.
We transform challenges into digital experiences
Get in touch to let us know what you’re looking for. Our policy includes 14 days risk-free!
Free project consultation