1. Introduction
In the previous session, we learned how to retrieve all the keys stored in Redis. In this lecture, we’ll focus on another essential operation — deleting keys.
Deleting keys is a simple but powerful operation. It allows you to remove unnecessary data, free up memory, or reset the database entirely when needed.
Let’s go step by step through the different ways you can remove data in Redis.
2. Viewing Existing Keys
Before deleting, let’s first look at the keys we currently have in Redis.
You can list all keys that match a certain pattern using the KEYS
command.
For example:
KEYS user*
Output:
1) "user:1:name" 2) "user:2:name" 3) "user:3:name"
This command lists all keys starting with the prefix user
.
These represent, for instance, user-related records we created earlier.
3. Deleting a Key
The command used to remove a key in Redis is DEL
.
The basic syntax is:
DEL key_name
Let’s delete a key named user:1:name
:
DEL user:1:name
Output:
(integer) 1
Redis returns an integer result indicating how many keys were deleted.
If the output is (integer) 1
, it means one key was successfully deleted.
If you try to delete a key that doesn’t exist, Redis will return (integer) 0
, meaning no keys were removed.
DEL user:10:name
Output:
(integer) 0
This simply means that Redis searched for the key user:10:name
but couldn’t find it.
4. Deleting Multiple Keys at Once
You can delete multiple keys in a single command by providing them one after another.
DEL user:2:name user:3:name
Output:
(integer) 2
Redis confirms that two keys were deleted successfully.
This is an efficient way to clean up multiple known keys without running separate delete commands.
5. Deleting All Keys from the Database
During development or testing, you might want to clear the entire database and start fresh.
Redis provides a convenient command for this purpose:
FLUSHDB
Output:
OK
The FLUSHDB
command deletes all keys from the currently selected Redis database, effectively resetting it.
Now, if you try to list the keys again:
KEYS *
Output:
(empty array)
This confirms that the database is now empty.
6. Understanding Key Patterns in Delete Commands
One common question developers have is whether they can delete multiple keys using a pattern (for example, user:*
).
The answer is no — the DEL
command does not support pattern matching.
You must specify the exact key names to delete.
Let’s understand this with an example:
DEL user:*
Output:
(integer) 0
Redis treats user:*
as a literal key name — not as a pattern — so unless such a key actually exists, it won’t delete anything.
If you want to remove multiple keys by pattern, you must first retrieve them using the KEYS
command and then delete them using DEL
.
Example:
KEYS user:*
Output:
1) "user:4:name" 2) "user:5:name"
Then delete them:
DEL user:4:name user:5:name
Output:
(integer) 2
This two-step process is the standard way to remove keys that match a certain prefix or pattern.
7. Checking the Number of Keys Before and After Deletion
You can easily check how many keys exist in your Redis database before and after deletion to confirm the effect.
KEYS *
Output:
1) "user:6:name" 2) "user:7:name" 3) "user:8:name"
After deleting a few keys:
DEL user:6:name user:7:name
Output:
(integer) 2
If you run the KEYS *
command again, you’ll see fewer keys:
1) "user:8:name"
Redis has removed the specified keys successfully.
8. Clearing All Databases
While FLUSHDB
clears the current database, Redis also supports multiple logical databases (usually numbered from 0 to 15).
To clear all of them at once, you can use:
FLUSHALL
Output:
OK
This command deletes every key from every Redis database, so use it carefully — especially in production environments.
9. Summary
Let’s summarize what we learned in this lecture:
- The
DEL
command is used to delete one or more keys. - The return value of
DEL
is the number of keys that were actually deleted. - If the key doesn’t exist, the return value is
0
. - The
DEL
command does not support patterns — it requires the exact key names. - To delete keys by pattern, first fetch them using
KEYS
, then pass them toDEL
. - The
FLUSHDB
command removes all keys from the current database. - The
FLUSHALL
command removes all keys from all databases.