Are you trying to delete all keys with prefix in Redis? We can help you do it easily.
If we want to delete a bunch of keys that match a common prefix, we delete them by pattern.
At Bobcares, we often receive requests to delete keys as part of our Server Management Services.
Today, let’s discuss how our Support Engineers do this easily for our customers.
A brief look at Redis
Before getting deep into the error, let us have a look at Redis.
Redis is a server that stores data. And, it primarily works as a key-value store, that can function as a database, cache and a message broker.
Also, it is non-relational. So, at its simplest level, Redis has been configured as basic key/value pairs.
Moreover, we can insert and retrieve keys in Redis using the SET and GET commands respectively.
How we delete all keys with prefix in Redis?
Having a decade of experience in managing servers, our Dedicated Engineers deal with the Redis queries frequently.
So, today let us see how we efficiently use the delete command to delete keys with a pattern.
Sometimes, while debugging code that uses Redis, we try to delete a set of keys with a common prefix.
Since these keys have a pattern common between them, deleting them altogether is more flexible.
Recently, one of our customers tried to purge all the keys with a particular prefix in his Redis database. Redis does not offer any method to delete bulk keys.
So, our Support Engineers used the below command to quickly delete all keys that match a certain pattern “$PATTERN”.
redis-cli --raw keys "$PATTERN" | xargs redis-cli del
Here, xargs takes the output of the first command and then process it with the redis-cli command. And, DEL command will delete many keys in a single command.
For instance, in the below example we delete all the keys with a pattern “key1“.
[Need more assistance with deleting keys?- We are here to help you.]
Conclusion
In today’s writeup, we saw how our Support Engineers delete all keys with prefix in Redis for our customers.
0 Comments