Skip to main content
0 reactions

Valkey FLUSHDB vs FLUSHALL: What’s the Difference?

valkey-cli session showing FLUSHDB emptying database 1 while database 0 keeps one key, then FLUSHALL emptying every database.
FLUSHDB empties one database; FLUSHALL empties them all.

Data loss: both commands delete keys for good. Never run them on a server that holds data you need. This guide uses a test Valkey on its own computer.

FLUSHDB and FLUSHALL both delete keys in Valkey®. The difference is how much they delete: one database, or all of them.

The Short Answer

  • FLUSHDB deletes every key in the database you’re in.
  • FLUSHALL deletes every key in every database on the server.

Syntax

Plain text
FLUSHDB [ASYNC | SYNC]
FLUSHALL [ASYNC | SYNC]
  • ASYNC: delete in the background, so Valkey keeps answering other clients meanwhile.
  • SYNC: finish deleting before replying. With neither, Valkey deletes this way unless its lazyfree-lazy-user-flush setting is yes.

From the FLUSHDB (opens in a new tab) and FLUSHALL (opens in a new tab) documentation.

Prerequisites

Step 1: Put a Key in Two Databases

Bash
valkey-cli
Plain text
SET book:4:title "Salt and Starlight"
Plain text
OK
Plain text
SELECT 1
Plain text
OK
Plain text
SET cart:42 "book:4"
Plain text
OK
Plain text
DBSIZE
Plain text
(integer) 1

DBSIZE counts the keys in the current database. Database 1 has one key, and database 0 has one too.

Step 2: Empty One Database with FLUSHDB

Plain text
FLUSHDB
Plain text
OK
Plain text
DBSIZE
Plain text
(integer) 0
Plain text
SELECT 0
Plain text
OK
Plain text
DBSIZE
Plain text
(integer) 1

FLUSHDB emptied database 1 only. The key in database 0 is still there.

Step 3: Empty Every Database with FLUSHALL

Plain text
FLUSHALL
Plain text
OK
Plain text
DBSIZE
Plain text
(integer) 0

FLUSHALL deleted the keys of every database on the server.

valkey-cli session showing FLUSHDB emptying database 1 while database 0 keeps one key, then FLUSHALL emptying every database.
FLUSHDB empties one database; FLUSHALL empties them all.

FLUSHDB vs FLUSHALL at a Glance

FLUSHDBFLUSHALL
DeletesEvery key in the current databaseEvery key in every database
Other databasesUntouchedEmptied too
OptionsASYNC or SYNCASYNC or SYNC

Clean Up

There’s nothing left to delete. Leave valkey-cli:

Plain text
exit

Common Mistakes

MistakeWhat happensDo this instead
Running FLUSHALL when you meant FLUSHDBEvery database on the server is emptiedUse FLUSHDB to clear one database
Running either command in the wrong placeYou delete keys you neededCheck the host, port and [n] in the prompt before you press Return

FAQ

Can I undo FLUSHALL?

Not with a command: the keys are gone. Part 21 of the Valkey Tutorial covers backups.

Is there a safer way to delete just a few keys?

Yes. Delete them by name with DEL, as in Part 2 of the Valkey Tutorial.

Conclusion

FLUSHDB empties the database you’re in, and FLUSHALL empties them all. Both are permanent, so check where you are first.

Additional Resources

Valkey® is a registered trademark of LF Projects, LLC. Redis® is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with the Valkey project, LF Projects or Redis Ltd., and isn’t endorsed, supported or certified by them.

0 comments