PHP Redis: Delete All Keys (Detailed Guide w/ Code Examples)

Use Case(s)

A common use case for deleting all keys in a Redis database using PHP is when you want to clear a database during development or testing, or to reset the state of your application.

Another use case might be when you need to purge old data that's no longer relevant. This action should be used with caution in a production environment as it will remove all data from the selected Redis database.

Code Examples

Here is a simple example of how you can delete all keys using the Predis client in PHP:

require_once 'Predis/Autoloader.php'; Predis\Autoloader::register(); $client = new Predis\Client(); $client->flushdb();

In this example, flushdb command is used. It removes all keys from the currently selected database.

Best Practices

  • Be very cautious while using the FLUSHDB or FLUSHALL commands. These commands will delete all keys and their associated data in the current or all databases respectively.

  • It's better to avoid the usage of these commands in a production environment. If it's inevitable, make sure you have a backup before running them.

Common Mistakes

  • One common mistake is not being aware of the current database that the Redis client is connected to. The command FLUSHDB deletes all keys from the currently selected database, not necessarily the one you might think it is.

  • It's also a mistake to use these commands without understanding the implications fully – they are irreversible and could lead to loss of critical data.


  1. Can I undo a FLUSHDB or FLUSHALL command? No, these commands cannot be undone. Be sure you want to delete the keys before running these commands.

  2. What's the difference between FLUSHDB and FLUSHALL in Redis? FLUSHDB deletes all keys from the currently-selected database, while FLUSHALL deletes all keys from all databases.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.