Development

How to Clear Redis Cache

In this tutorial, you will learn how to clear the Redis cache by removing all the keys in a given Redis database using the CLI utility.
Captain Salem 1 min read
How to Clear Redis Cache

Redis is a free, open-source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.

Redis Clear Cache with the Redis CLI

Redis provides a CLI interface that allows us to interact with the Redis server using raw commands in a terminal session.

we can use the Redis CLI commands to remove all the keys from a single or all databases in the cluster.

Redis Delete All Keys From Specific Database

In Redis, we can use the flushdb command to remove all the keys from a specific database. The command syntax is as shown:

redis-cli flushdb

Calling the flushdb command without arguments will remove all the keys from the currently selected database.

To remove all the data stored in a target Redis database, use the -n option; the syntax is as shown:

redis-cli -n [database_index] flushdb

Where the database_index is the target database ranging from 0 to 16.

Since Redis version 4.0+, we can delete keys from a given database in the background without blocking the server. This is possible using the asynchronous option specified using the ASYNC parameter.

Command syntax:

redis-cli FLUSHDB ASYNC
redis-cli -n <database_index> FLUSHDB ASYNC

Redis Delete All Keys

To delete all the keys from the Redis cluster (across all databases), we can use the FLUSHALL command as shown:

redis-cli flushall

To operate asynchronously, run the following:

redis-cli flushall async

Conclusion

This tutorial shows how to remove all the keys from a given database. You also learned how to clear the Redis cache by removing all the data across the Redis databases.

If you enjoy our content, please consider buying us a coffee to support our work:

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.