Delete All Keys in Redis Using Ruby (Detailed Guide w/ Code Examples)

Use Case(s)

In a number of instances, you might need to delete all keys from your Redis database. For example, during testing, you may want to clear the database to reset the environment. Or you may wish to remove all keys as part of a caching strategy when the data becomes invalid or stale.

Code Examples

Consider you have a connection established with your Redis instance using the redis gem in Ruby. To delete all keys, you can use the flushdb method, which removes all keys from the current database:

require 'redis' redis = redis.flushdb

This code will empty the current selected database. It's worth noting that flushdb operates on the currently selected database only.

If you want to delete keys across all databases, use the flushall method:

require 'redis' redis = redis.flushall

This snippet will erase all keys in every database on the Redis server.

Best Practices

  • Always be cautious when using flushdb or flushall, as these operations are not reversible.
  • Avoid executing these commands in a production environment unless absolutely necessary.
  • Confirm that you're connected to the correct Redis server to avoid unintended loss of data.

Common Mistakes

  • Accidentally running flushall or flushdb on a production database.
  • Not confirming the selected database before executing flushdb.


  1. Does flushdb affect other databases on the server? No, flushdb only removes keys from the currently selected database.
  2. Is there a way to undo flushall or flushdb? No, once executed, these operations are irreversible.

Was this content helpful?

Start building today

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