How to Clear Redis Cache

October 26, 2020

Introduction

Redis is an open-source solution for data structure storage. It is primarily used as a key-value store, which allows it to work as a database, cache storage, and message broker.

In this tutorial we will cover different ways you can delete these key-values (keys) and clear Redis cache.

How to clear Redis cache

Prerequisites

Clear Redis Cache With the redis-cli Command

The easiest way to clear Redis cache is to use the redis-cli command.

Databases in Redis are stored individually. Using the redis-cli command allows you to either clear the keys from all databases, or from a single specified database only.

redis-cli Command Syntax

The redis-cli command uses the following syntax:

redis-cli [database number] [option]

Where:

  • [option] – Lets you choose between clearing all databases or one specific database of your choice.
  • [database number] – Lets you specify which database you want to clear.

Note: Once you delete keys from a database, they can no longer be recovered.

Deleting All Keys

To delete keys from all Redis databases, use the following command:

redis-cli flushall
Deleting all keys

As of version 4.0.0, Redis can clear keys in the background without blocking your server. To do this, use the flushall command with the async parameter:

redis-cli flushall async

Deleting Keys from a Specific Database

Use the following command to clear a specific database only:

redis-cli flushdb

Using the flushdb command without any parameters clears the currently selected database. Use the -n parameter with the database number to select a specific database you want to clear:

redis-cli -n [database number] flushdb

You can also use the async option when clearing keys from individual databases:

redis-cli -n [database number] flushdb async

Automating Cache Clearing Using Ansible

If you have a large number of Redis servers running, clearing the cache for each one manually takes time.

To speed this process up, use a tool like Ansible to clear the cache on all your Redis servers at the same time:

ansible all -m command -a '/usr/bin/redis-cli flushall '

Running this command applies the flushall command to every server in your Ansible inventory file:

  • all – Lets you choose all the remote hosts in the Ansible inventory file.
  • -m – Lets you choose a module to execute.
  • -a – Provides an argument for the module. In this case, the command module runs the flushall command with redis-cli.
Automate clearing cache using Ansible

Note: To get started with Ansible, please refer to our installation guides How To Install Ansible On Ubuntu 20.04 or How To Install Ansible On Windows.

Conclusion

After going through this tutorial, you have learned how to use the redis-cli and flush commands to clear your Redis cache.

Next, learn more about Redis by exploring Redis Data Types.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
Redis Data Types with Commands: Comprehensive Guide
September 4, 2020

The article covers seven basic Redis Data Types, including HyperLogLogs and Bitmaps. Each data type is...
Read more
SQL vs NoSQL: What's the Difference?
August 25, 2020

There are two main types of database solutions: SQL and NoSQL. In this article, you will learn the key...
Read more
How to Deploy and Run Redis in Docker
July 23, 2020

The tutorial shows you how to deploy Redis using the Docker run command. Also, learn how to deploy Redis on...
Read more
How to Install Redis on Ubuntu 20.04 / 18.04
July 7, 2020

Install Redis on Ubuntu 20.04 in a couple of simple steps by following this tutorial. This guide also shows...
Read more