Redis

How to Use Delete All Keys in a Redis Cluster

Redis is a free, open-source key-value pair database. It stores the data in memory, making it incredibly fast and popular for high-performance environments.

It is utilized by small to large applications, including Twitter, Github, StackOverflow, and many more. In some cases, you may need to reset all the databases stored in your Redis cluster and start fresh.

This article will discuss deleting all the data stored in a Redis database.

Redis Delete All Keys

The Redis FLUSHALL command lets you delete all the keys stored in the databases in the Redis instance.

For example, to delete all the keys in the database at index 0, use the command:

127.0.0.1:6379> flushall

OK

The command returns ok if the operation is completed successfully.

You do not need to log in to the Redis-CLI. You can run an ad-hoc command as shown below:

$ redis-cli flushall

OK

Async Delete

The FLUSHALL command is blocking. This means that you will have to wait for the flush operation to complete before running other functions on the server.

However, as of Redis version 4.0 and above, you can run a non-blocking flush operation using the ASYNC parameter.

The following example is provided:

127.0.0.1:6379[10]> FLUSHALL ASYNC

OK

The ASYNC parameter forces the flush operation to run in the background without blocking the server.

Delete Keys in the Current Selected Database

To only remove the keys in the selected database, use the flushdb command. An example is as shown below:

127.0.0.1:6379[10]> flushdb

OK

In this case, we delete all the keys in the currently selected database, database 10.

The ad-hoc command is provided below:

$ redis-cli -n 10 flushdb

OK

The previous command uses the -n parameter to specify which database to run the flushdb command.

The command also supports the asynchronous operation.

$ redis-cli -n 10 flushdb async

Conclusion

This tutorial discussed how to delete all the keys in a Redis cluster or a specific database. In addition, FLUSHALL command and the flushdb command were explained with examples provided. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list