Redis is a powerful in-memory data structure store that works by storing the key-value pairs. It’s a flexible solution to be utilized as a database, cache, message broker and more. It’s a free and open-source software with incredible popularity.
In this guide, we will look at deleting a key from the Redis database.
Prerequisites
To perform the steps demonstrated in this guide, you will need the following components:
- A properly-configured Linux system. Learn more about installing Ubuntu.
- The latest version of Redis server installed and configured properly. Check out installing Redis on Ubuntu.
Redis Key
Redis works using the key-value pairs. Basically, a key is a reference to a data point that Redis stores. When working with the data, you must provide the key.
To manage the keys, Redis comes with numerous commands. For example, the SET command is used to declare a key-value pair in the Redis database. The GET command retrieves the value of the key (in string format). The KEYS command searches the list of keys registered and prints the results that matches the given pattern. The EXISTS command checks if a key exists in the Redis server.
To delete a key, Redis comes with the DEL command. There’s also the GETDEL command that prints the key value on the screen before deleting the key from the Redis database.
Creating a Key-Value Pair
For demonstration, we are going to create a simple key-value pair using the SET command. Launch the Redis CLI using the followingcommand:
1 | $ redis-cli |
From the console, run the following command:
1 | $ SET random "the quick brown fox" |
This command registers a new key random with the string value “the quick brown fox” on the Redis database. We can verify if the registration was successful using the EXISTS command:
1 | $ EXISTS random |
Try retrieving the key using the GET command:
1 | $ GET random |
Deleting a Key
Using the DEL Command:
To delete the key, use the DEL command:
1 | $ DEL <key> |
To delete multiple keys, the following DEL command supports multiple arguments:
1 | $ DEL <key_1> <key_2> <key_3> |
If a key doesn’t exist in the Redis database, the GET command returns nil. Taking advantage of this behavior, we can test if the key deletion was successful using the following command:
1 | $ GET <deleted_key> |
Using the GETDEL Command:
An alternative way of deleting keys is using the GETDEL command. Before deleting the key from the database, GETDEL prints the value of the key on the screen.
However, it’s seemingly limited in functionality. If the key doesn’t exist, GETDEL returns nil. If the key value is not a string, it returns an error.
Apply the following GETDEL command in our previous example:
1 | $ GETDEL <key> |
We can use the GET command to verify if the key deletion was successful. Here, the expected return value is nil:
1 | $ GET <deleted_key> |
Deleting Keys by Pattern
In a real-life situation, Redis is handling numerous key-value pairs in its database. Deleting unwanted values one by one is nigh impossible. What do you do in such a situation?
Unfortunately, Redis does not come with a dedicated command to perform this action. So, we have to get a bit creative and involve some Bash scripting.
First, we need a way to get a list of keys matching a specific pattern. The following command achieves this goal:
1 | $ redis-cli --scan --pattern <pattern> |
Notice that we don’t need to access the Redis shell to extract the result. Next, we need to pass this list to Redis for key deletion:
1 | $ redis-cli --scan --pattern <pattern> | xargs redis-cli DEL |
Here, the list of keys is piped to xargs. The xargs command takes the list and runs the specified command for each entry on the list.
Conclusion
In this guide, we explored the various ways in which we can delete the keys from Redis. We demonstrated how to delete the Redis keys one by one. We also demonstrated how to delete the Redis keys by pattern.
Interested in learning more about Redis? The Redis sub-category contains hundreds of tutorials on various components of Redis. Learn more about LUA scripting with Redis, Redis with Ruby, Redis pipelining etc.