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
FLUSHDBdeletes every key in the database you’re in.FLUSHALLdeletes every key in every database on the server.
Syntax
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 itslazyfree-lazy-user-flushsetting isyes.
From the FLUSHDB and FLUSHALL documentation.
Prerequisites
- A test Valkey with no data you need, for example the one from How to Install Valkey on macOS.
- A terminal.
Step 1: Put a Key in Two Databases
valkey-cli
SET book:4:title "Salt and Starlight"
OK
SELECT 1
OK
SET cart:42 "book:4"
OK
DBSIZE
(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
FLUSHDB
OK
DBSIZE
(integer) 0
SELECT 0
OK
DBSIZE
(integer) 1
FLUSHDB emptied database 1 only. The key in database 0 is still there.
Step 3: Empty Every Database with FLUSHALL
FLUSHALL
OK
DBSIZE
(integer) 0
FLUSHALL deleted the keys of every database on the server.
FLUSHDB vs FLUSHALL at a Glance
| FLUSHDB | FLUSHALL | |
|---|---|---|
| Deletes | Every key in the current database | Every key in every database |
| Other databases | Untouched | Emptied too |
| Options | ASYNC or SYNC | ASYNC or SYNC |
Clean Up
There’s nothing left to delete. Leave valkey-cli:
exit
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Running FLUSHALL when you meant FLUSHDB | Every database on the server is emptied | Use FLUSHDB to clear one database |
| Running either command in the wrong place | You delete keys you needed | Check 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.
Related
- Valkey Tutorial — Part 2: Your First Valkey Commands with valkey-cli
- How to Switch Databases in Valkey with SELECT
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.