Redis FLUSHDB: Quick Guide and Other Ways to Clear Redis Cache
The FLUSHDB command in Redis is used to delete all keys in the currently selected database. This operation is irreversible, so it should be used with caution.
October 8, 2025

What is Redis FLUSHDB Command?
The FLUSHDB command in Redis is used to delete all keys in the currently selected database. This operation is immediate and irreversible. It does not affect keys in other logical databases within the same Redis instance. One can switch to a different logical database using the SELECT command.
FLUSHDB is useful when you need to clear a specific Redis database without shutting down the server or affecting other applications that may be using different logical databases. It's commonly used in development, testing, or administrative scripts that need to reset a Redis database to an empty state.
A similar command is FLUSHALL, which clears all data across all databases on the current Redis server.
Warning: Because they wiped out all data, FLUSHDB and FLUSHALL should be used with caution in production environments.
Understanding Redis FLUSHDB Syntax
The basic syntax of the FLUSHDB command is:
FLUSHDB [ASYNC | SYNC]- Without any argument,
FLUSHDBruns synchronously by default and clears the selected database immediately. Starting with Redis 6.2, thelazyfree-lazy-user-flushconfiguration can change the default flush mode to asynchronous. - Using the
ASYNCoption performs the deletion in the background, allowing Redis to continue handling other commands while it clears the data. - The
SYNCoption explicitly forces a synchronous flush, although this is the default behavior.
These options are useful for tuning the performance of the flush operation depending on your workload and latency requirements.
Cleaning Cache Using a Redis Client Library
If you are using a Redis client library in your programming language, you can call the appropriate method to execute the flush command. Here is an example using Python's Redis library:
import redis
# Connect to Redis
r = redis.Redis(host='localhost', port=6379, db=0)
# Clear the current database's data
r.flushdb()
# OR to clear all databases' data
r.flushall()Warning: Remember that clearing the cache will remove all stored data, so use these commands with caution.
Key Use Cases for Using the Redis FLUSHDB Command
Cache Reset
In caching scenarios, Redis stores computed or fetched data to reduce load on backend systems and improve response times. Over time, the cache may become outdated due to changes in business logic, data schemas, or external data sources. FLUSHDB provides a simple way to clear all cache entries in the selected Redis database, ensuring that stale or inconsistent data is removed.
Memory Reclamation
Redis is an in-memory datastore, so its memory footprint can grow rapidly if old or unused keys aren't evicted. In scenarios where automatic eviction policies aren't suitable, FLUSHDB can be used to proactively reclaim memory. For example, after a temporary data processing job or batch import, the database may no longer need to retain large datasets. This is essential for managing memory usage in multi-tenant Redis setups or environments with tight memory limits.
Migration / Cutover
Before migrating data into Redis or switching to a new application version, it's often necessary to ensure that the Redis database is in a clean state. FLUSHDB helps remove all keys from the target database to prevent interference from residual or legacy data.
During a cutover process, this ensures that only fresh, valid data is served post-deployment. It also minimizes the risk of data conflicts, key collisions, or inconsistencies when loading snapshots, restoring backups, or performing zero-downtime transitions.
Development and Testing
During development, engineers frequently modify data structures, key names, or access patterns. FLUSHDB allows developers to reset the database to an empty state, making it easier to test code in a predictable environment.
Automated test suites also benefit from this command to isolate test cases and prevent data leakage across test runs. It simplifies environment setup and teardown in CI/CD pipelines, improving reliability and repeatability of integration and system tests.
Redis FLUSHDB vs. Other Commands
FLUSHDB vs. FLUSHALL
While FLUSHDB removes all keys from the currently selected Redis database, FLUSHALL deletes keys from all databases in the Redis instance. This makes FLUSHALL a more destructive operation, as it affects the entire Redis server.
Use FLUSHALL only when you need a full reset across all databases, such as during integration testing or when decommissioning an instance. As with FLUSHDB, you can specify ASYNC or SYNC for background or immediate execution. Always confirm that no applications are using other databases before running FLUSHALL.
FLUSHDB vs. DEL
The DEL command deletes one or more specified keys, rather than wiping an entire database. It's ideal for targeted key removal, such as invalidating a specific cache entry or clearing a user session.
In contrast, FLUSHDB is a bulk operation that deletes everything in the current database. Use DEL when you know the exact keys to remove and want to avoid clearing unrelated data.
FLUSHDB vs. UNLINK
UNLINK works like DEL but performs the deletion asynchronously. Instead of blocking the server while removing the key and its memory, UNLINK unlinks the key reference and schedules actual memory reclamation in the background.
While FLUSHDB removes all keys in a database, either synchronously or asynchronously, UNLINK is used for selective, non-blocking key removal. If you need to delete specific large keys without affecting latency, UNLINK is more suitable. For full-database clears, FLUSHDB ASYNC offers similar non-blocking behavior.
Pro Tips for Using the Redis FLUSHDB Command
1. Double-Check You Are on the Correct Database
Redis supports multiple logical databases, typically numbered from 0 to 15 by default, and each database is isolated from the others. When you execute the FLUSHDB command, it only affects the currently selected database. This means that if you're connected to the wrong database, you might end up clearing unrelated data while leaving the intended target untouched.
In command-line interfaces, you can verify the selected database using the SELECT command or inspect your Redis client's configuration. Many client libraries allow specifying the database number during initialization. It's good practice to log or display the current database context in scripts or administrative tools before running FLUSHDB, especially in environments with multiple databases or shared infrastructure.
Adding an explicit check in automation scripts or admin consoles can prevent costly errors. For critical environments, consider implementing confirmation prompts or safeguards that require multiple steps before allowing database flushes.
2. Prefer ASYNC Mode When Possible to Reduce Blocking
By default, FLUSHDB operates synchronously, which means Redis processes the command and deletes all keys before moving on to the next command. For databases with large keyspaces or high memory usage, this operation can take noticeable time and temporarily block the Redis server from processing other commands. This can lead to increased latency or even timeouts for client applications.
The FLUSHDB ASYNC option addresses this by offloading the actual deletion to a background thread. Redis unlinks the keys from the keyspace immediately, making them invisible to subsequent commands, and then reclaims memory asynchronously. This approach ensures that the Redis server remains responsive, even under heavy load or when dealing with large datasets.
In performance-sensitive applications or real-time systems, using the ASYNC variant is strongly recommended to maintain service quality. It's particularly effective in scenarios where cache invalidation or test environment resets occur frequently.
3. Schedule Flushes During Low-Traffic Windows in Production
Clearing a Redis database is a disruptive operation, regardless of whether it's done synchronously or asynchronously. Even with FLUSHDB ASYNC, background deletion consumes CPU and memory resources, which can affect latency and throughput for other commands. In synchronous mode, the impact is even more pronounced, as Redis halts command processing until the flush is complete.
To minimize the risk of service degradation, schedule flush operations during maintenance windows or off-peak hours when traffic is minimal. Coordinate these windows with operations and application teams to ensure that any necessary service restarts or rebuild processes are planned accordingly.
Consider integrating FLUSHDB into scheduled jobs with alerts or logging, so that operations teams are aware of when a flush has occurred. This also helps with incident tracing in case application performance drops after the command is executed.
4. Rebuild from Source Data When Flushing Cached Data
Redis is often used as a volatile cache that can be repopulated from a more persistent backend, such as a relational database. If you use FLUSHDB to clear such a cache, it is critical that the application includes logic to rebuild the necessary data structures on demand or in bulk.
For example, you might implement lazy loading, where the application fetches and caches data when first accessed after a flush. Alternatively, you can use warm-up scripts that proactively repopulate the cache with commonly accessed data or large lookup tables.
Without a rebuild mechanism, a flushed cache could result in increased latency, failed requests, or degraded user experience as the application struggles to serve uncached content. In CI/CD pipelines or test environments, rebuilding the cache ensures consistent test results and reduces test flakiness due to missing data.
Dragonfly: Next-Gen In-Memory Data Store with Limitless Scalability
Dragonfly is a modern, source-available, multi-threaded, Redis-compatible in-memory data store that stands out by delivering unmatched performance and efficiency. Designed from the ground up to disrupt legacy technologies, Dragonfly redefines what an in-memory data store can achieve.
Dragonfly Scales Both Vertically and Horizontally
Dragonfly's architecture allows a single instance to fully utilize a modern multi-core server, handling up to millions of requests per second (RPS) and 1TB of in-memory data. This high vertical scalability often eliminates the need for clustering—unlike Redis, which typically requires a cluster even on a powerful single server (premature horizontal scaling). As a result, Dragonfly significantly reduces operational overhead while delivering superior performance.
For workloads that exceed even these limits, Dragonfly offers a horizontal scaling solution: Dragonfly Swarm. Swarm seamlessly extends Dragonfly's capabilities to handle 100 million+ RPS and 100 TB+ of memory capacity, providing a path for massive growth.
Key Advancements of Dragonfly
- Multi-Threaded Architecture: Efficiently leverages modern multi-core processors to maximize throughput and minimize latency.
- Unmatched Performance: Achieves 25x better performance than Redis, ensuring your applications run with extremely high throughput and consistent latency.
- Cost Efficiency: Reduces hardware and operational costs without sacrificing performance, making it an ideal choice for budget-conscious enterprises.
- Redis API Compatibility: Offers seamless integration with existing applications and frameworks running on Redis while overcoming its limitations.
- Innovative Design: Built to scale vertically and horizontally, providing a robust solution for rapidly growing data needs.
Dragonfly Cloud
Dragonfly Cloud is a fully managed service from the creators of Dragonfly, handling all operations and delivering effortless scaling so you can focus on what matters without worrying about in-memory data infrastructure anymore.
Was this content helpful?
Help us improve by giving us your feedback.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost