Wiping Data

Difficulty: Beginner

Question

What is the difference between FLUSHDB and FLUSHALL, and how do you use or protect against them safely in production?

Answer

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.

Side by side

FLUSHDBFLUSHALL
ScopeCurrent DB only (the one chosen with SELECT)All DBs on the server
Typical useClearing a test DB, a dedicated cache DBResetting a whole dev or staging server
OptionsASYNC / SYNCASYNC / SYNC
ReplicatedYes, replicas flush tooYes, replicas flush too
In Redis ClusterOnly DB 0 exists, so on a node it behaves like FLUSHALLAffects only the node you send it to

SYNC vs ASYNC

  • Synchronous flush walks and frees every key on the main thread. With tens of millions of keys that can mean a pause long enough to trip health checks and client timeouts.
  • 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.
  • The SYNC option (6.2+) forces synchronous behaviour, and the lazyfree-lazy-user-flush config (6.2+) decides what a flush without an option does.

Why these are dangerous in shared setups

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.

Protecting production

  1. ACLs (Redis 6+). Give application users -@dangerous or at least -flushall -flushdb. Only an admin user should have them.
  2. Rename or disable (older style). rename-command FLUSHALL "" in redis.conf disables the command. ACLs are the modern, more flexible approach.
  3. Managed services. Some providers restrict these commands or expose them only through their console.
  4. Prefer targeted deletes. To clear a cache namespace, iterate with SCAN 0 MATCH cache:* COUNT 1000 and delete batches with UNLINK, or version your key prefix (cache:v7:) so old keys simply expire.
  5. Backups. Keep RDB snapshots or AOF so an accidental flush is recoverable. Note that once the flush reaches the AOF, replaying it replays the flush too, so you need a copy from before it.
Which keys each flush removes. FLUSHDB touches only the selected database; FLUSHALL clears the whole server.
FLUSHDB (after SELECT 2)FLUSHALL
DB 0 (sessions)KeptDeleted
DB 1 (job queue)KeptDeleted
DB 2 (cache)DeletedDeleted
ReplicasDB 2 flushedEverything flushed

Code examples

Flushing without blocking, and locking it down

# 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.

Key points

  • FLUSHDB clears the selected DB; FLUSHALL clears all DBs on the server
  • Both are synchronous by default; ASYNC frees memory in a background thread
  • Flushes replicate to replicas and reach the AOF
  • Block them for app users with ACL -@dangerous or rename-command
  • Prefer SCAN plus UNLINK or prefix versioning to clear one namespace

Common follow-up questions

  • What is the difference between DEL and UNLINK?
  • How would you clear only keys matching cache:* without blocking Redis?
  • How do you recover from an accidental FLUSHALL?
  • Why is sharing one Redis instance between cache and queues risky?

Interview tips

  • State the scope difference first, then immediately mention ASYNC and blocking behaviour
  • Show production awareness: ACLs, shared-instance risk and backups
  • Offering SCAN plus UNLINK as the safer alternative usually impresses

Concepts covered

FLUSHDB, FLUSHALL, ASYNC flush, lazyfree, Dangerous commands

More Redis & Caching questions