How do I delete everything in Redis?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Redis is a powerful, in-memory data structure store, used as a database, cache, and message broker. When you are managing a Redis database, there might come a time when you need to delete all the keys in the database. This could be for testing purposes or just resetting the database to a clean state. Here are a few methods you can use to delete everything in Redis, depending on your specific needs.
Using the FLUSHDB and FLUSHALL Commands
FLUSHDB
The FLUSHDB command clears the database you are currently connected to. This is a straightforward way to delete all keys from the currently selected Redis database.
FLUSHALL
The FLUSHALL command goes a step further by clearing all keys from all databases on the Redis server. This is useful when you have multiple databases in Redis and need to clear all data in one step.
Asynchronous Versions
Redis also offers asynchronous versions of these commands, FLUSHDB ASYNC and FLUSHALL ASYNC. These commands do not block the server while deleting keys. Instead, the deletion of keys is done in the background, allowing Redis operations to continue.
This can be particularly useful in production environments where maintaining service availability and minimizing latency are crucial.
Using Keys Pattern Deletion
For more control over what gets deleted, you might prefer deleting keys that match a certain pattern. This can be done using a combination of SCAN and DEL commands.
- SCAN: Iterates over keys in the database.
- DEL: Deletes a list of specified keys.
Here’s a simple script that you use in redis-cli:
This will first find all keys that match the pattern and then delete them.
Handling Large Databases
In scenarios where the database size is large, using the FLUSHALL or FLUSHDB commands could lead to performance issues due to high memory and CPU usage while Redis is deleting all keys. In such cases, it may be beneficial to delete keys in batches using a cursor-based iteration with the SCAN command.
Key Points Summary
| Command | Description | Use Case |
FLUSHDB | Deletes all keys in the current database. | When clearing data from a single DB. |
FLUSHALL | Deletes all keys in all databases on the server. | When resetting the entire Redis server. |
FLUSHDB ASYNC | Asynchronous deletion of all keys in the current database. | Minimize latency impact in production. |
FLUSHALL ASYNC | Asynchronous deletion of all keys in all databases on the server. | For non-blocking operations server-wide. |
SCAN and DEL | Iterate and delete keys by matching patterns. | Precise deletion based on key patterns. |
Safety and Precautions
Before executing these commands, especially in a production environment, ensure that:
- You have confirmed the data to be deleted (avoid accidental massive data loss).
- Backups are taken if the data might be needed in the future.
- The impact on the application and user experience is considered.
Conclusion
Deleting all data in Redis is a powerful operation and can be executed in several ways, each suitable for different scenarios. Whether you need a complete wipe or a selective delete, Redis offers the tools necessary to handle data management efficiently. Always exercise caution with data deletion operations to prevent unintended loss of important data.
Related reading
- How do I do a bulk insert in mySQL using node.js
- How do I do a fuzzy match of company names in MYSQL for auto-complete?
- How do I drop a MongoDB database from the command line?
- How do I enable EF migrations for multiple contexts to separate databases?
- How do I enlarge an EER Diagram in MySQL Workbench?
- How do I escape a single quote in SQL Server?
- How do I escape reserved words used as column names? MySQL/Create Table
- How do I extract the created date out of a Mongo ObjectID

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.