How to Clear Redis Cache: A Step-by-Step Guide

Try this guide with our instant dedicated server for as low as 40 Euros

clear Redis cache

Redis is an open-source, in-memory data structure store used as a database, cache, and message broker. 

Unlike traditional databases that store data on disks, Redis stores data in RAM. While it can store simple data types like strings, Redis goes beyond this basic capability. It supports various complex data structures like lists, sets, sorted sets, and hashes, making it a flexible component for data storage and access. 

Its combination of flexibility and functionality makes it a popular and powerful choice for caching data and objects. 

After some time, the Redis cache could accumulate junk that takes up space, hinders performance and user experience, and slows down the response time. As such, it is necessary to refresh the Redis cache to clear out the junk and ensure sustained, smooth performance. 

In this tutorial, you will learn how to clear the Redis cache in three simple steps.

Table Of Contents

  1. How to Clear Redis Cache
    1. What is redis-cli?
    2. Automate Cache Clearing Using Ansible
  2. Conclusion
  3. FAQs

How to Clear Redis Cache

The most reliable method of clearing the Redis cache is through the redis-cli. Before we go into the steps of the process, let’s take a quick look at the redis-cli.

What is redis-cli? 

redis-cli is a command-line tool that allows you to interact with a Redis server in a terminal. You can clear the keys from all databases or specify a single database for the clean-up.

Note: Clearing the Redis cache will remove all the keys in the currently selected database. Therefore, ensure that you have selected the right database and have backed up the data.

Start by logging into the Redis server. Here, you can opt for one of the following methods:

  • If your Redis server is running on a remote machine, you can connect using SSH and then use the redis-cli command. 
  • If Redis is installed locally on your machine, you can connect directly using redis-cli without SSH.

Once you are logged into the Redis server, you can now run Redis commands.

Clear the Database

You should first select the database you need to work with. The database number is usually 0 (zero) by default unless explicitly changed. 

To find the database number, run:

# redis-cli CONFIG GET databases

Once you get the database number, run the command below to clear the database:

# redis-cli [database number] [option]


  • [option] allows you to decide if you want to clear all databases or just one specific database.
  • [database number] lets you choose the database you want to clear.

Delete All Keys

To clear all keys from the Redis databases, run this command:

# redis-cli flushall

redis-cli flushall

From version Redis 4.0.0 onwards, keys can be cleared without affecting your server with little impact on the performance. To accomplish this, use the flushall command with the async parameter:

# redis-cli flushall async

Delete Keys from a Specific Database

Alternatively, you can run the following command syntax to clear keys from the currently selected database:

# redis-cli flushdb

redis-cli flushdb

The specific command in this scenario will be as follows: 

# redis-cli -n [database number] flushdb

Replace [database number] with the desired database number.

To clear keys from individual databases, run the following command: 

redis-cli -n [database number] flushdb async 

Automate Cache Clearing Using Ansible

The granular control over the process of clearing specific databases is great. However, manually managing multiple Redis instances or databases can be tedious. 

Here, automating cache clearing is often an important aspect of Redis database maintenance. The process saves your time and effort and minimizes the risk of application downtime caused by a full cache. 

Ansible is a powerful automation tool that can help you streamline various tasks, including automating cache clearing in Redis. 

Follow these steps to automate cache clearing using Ansible:

Install Ansible on Your System

The actual steps of installing Ansible depend on the operating system of your server.

We strongly recommend referring to Ansible documentation for detailed instructions on installation.

Create an Ansible Playbook

Create a playbook with tasks for clearing the Redis cache. Note that you need to follow a specific process (outlined in the official documentation) to create an Ansible playbook. 

Use the command module within Ansible to execute Redis commands like flushall or flushdb.

Specify the target hosts where you want to clear the Redis cache in your playbook.

Run the Playbook

Use the ansible-playbook command to automate the cache-clearing process.
# ansible-playbook clear_redis_cache.yml

You can now simplify cache clearing in Redis, thereby saving time and ensuring consistent management across your infrastructure.


Clearing the Redis cache is crucial for maintaining your application performance and reliability. By following the steps outlined in this guide, you can effectively clear the Redis cache using the redis-cli command-line interface and automate the process with Ansible. 

Remember to exercise caution when clearing the cache, especially when using commands like flushall to delete all keys from Redis. With the right knowledge and tools, you can keep your Redis cache clean and optimize your application’s performance.


Q. How do I clear the Redis cache using the flushall command?

To clear the Redis cache using the flushall command, you can simply run the following command in your Redis console: redis-cli flushall. This command removes all keys from all databases in the Redis.

Q. Can I delete all keys from a specific Redis database?

Yes, you can delete all keys from the currently selected database in Redis by executing the flushdb command. 

Q. What is Redis Cluster and how does it impact clearing cache?

Redis Cluster is a distributed implementation of Redis that allows you to automatically share data across multiple Redis nodes. When clearing the cache in a Redis Cluster, ensure that you clear the cache on all nodes to completely flush Redis.

Q. How do I connect to a Redis instance to clear the cache?

To connect to a Redis instance and clear the cache, you can use the redis-cli command in your terminal. Once connected, you can run the appropriate commands to flush the cache.

Q. Is Redis used only as a cache or can it function as a database as well?

Redis is an open-source, in-memory data structure store that can be used both as a cache and a database. Though it is commonly used as a cache due to its high performance, it can also be used as a primary database for certain use cases.

Q. What is the difference between flushdb and flushall commands in Redis?

The flushdb command in Redis removes all keys from the currently selected database, while the flushall command removes all keys from all databases in the Redis instance. It’s important to use these commands carefully to avoid accidental data loss.

Q. Can I clear the Redis cache in a Redis Cluster environment?

Yes, you can clear the Redis cache in a Redis Cluster environment by connecting to each node in the cluster and running the appropriate commands to flush the cache on each node. This ensures that the cache is completely cleared across the entire cluster.

Q. What is the recommended approach for clearing the Redis cache in a production environment?

In a production environment, it is recommended to carefully plan and schedule cache-clearing operations to minimize any impact on the application’s performance. You can use tools like Redis Cluster or AWS ElastiCache to efficiently manage and clear the cache without affecting the application performance.

Try this guide with our instant dedicated server for as low as 40 Euros