Difficulty: Beginner
What is the difference between FLUSHDB and FLUSHALL, and how do you use or protect against them safely in production?
FLUSHDB deletes every key in the currently selected logical database, while FLUSHALL deletes every key in every database on that Redis server. Both are irreversible unless you have a backup, and by default both run synchronously, so on a large dataset they can block Redis for a noticeable time. Add ASYNC to free memory in a background thread.
FLUSHDB | FLUSHALL | |
|---|---|---|
| Scope | Current DB only (the one chosen with SELECT) | All DBs on the server |
| Typical use | Clearing a test DB, a dedicated cache DB | Resetting a whole dev or staging server |
| Options | ASYNC / SYNC | ASYNC / SYNC |
| Replicated | Yes, replicas flush too | Yes, replicas flush too |
| In Redis Cluster | Only DB 0 exists, so on a node it behaves like FLUSHALL | Affects only the node you send it to |
FLUSHALL ASYNC (Redis 4.0+) swaps in an empty keyspace immediately and frees the old one in a background thread, so the command returns quickly.SYNC option (6.2+) forces synchronous behaviour, and the lazyfree-lazy-user-flush config (6.2+) decides what a flush without an option does.People often share one Redis between a cache and something important: Sidekiq or BullMQ job queues, rate-limit counters, sessions. Running FLUSHALL to "clear the cache" also wipes queued jobs and logs out every user. Even FLUSHDB is risky when two apps share DB 0.
A real failure mode: a developer connects to what they think is staging, runs FLUSHALL, and it was production because both use the same hostname pattern.
-@dangerous or at least -flushall -flushdb. Only an admin user should have them.rename-command FLUSHALL "" in redis.conf disables the command. ACLs are the modern, more flexible approach.SCAN 0 MATCH cache:* COUNT 1000 and delete batches with UNLINK, or version your key prefix (cache:v7:) so old keys simply expire.| FLUSHDB (after SELECT 2) | FLUSHALL | |
|---|---|---|
| DB 0 (sessions) | Kept | Deleted |
| DB 1 (job queue) | Kept | Deleted |
| DB 2 (cache) | Deleted | Deleted |
| Replicas | DB 2 flushed | Everything flushed |
# Clear only the cache DB, freeing memory in the background
SELECT 2
FLUSHDB ASYNC
# Application user that cannot run dangerous commands
ACL SETUSER app on >S3cretPass ~* &* +@all -@dangerous
# Safer than a flush: delete one namespace in batches
# (run from a shell, not inside redis-cli)
# redis-cli --scan --pattern 'cache:*' | xargs -n 500 redis-cli UNLINK
ASYNC avoids a long main-thread pause. The ACL rule removes FLUSHALL, FLUSHDB, KEYS, DEBUG and other dangerous commands from the app user.
FLUSHDB, FLUSHALL, ASYNC flush, lazyfree, Dangerous commands