fbpx

How To Clear Redis Cache from Command Line (2 Easy Methods)

Updated on May 14th, 2022
by Shoaib Khan

Redis is an open-source, in-memory, data structure store. It can be used as a database, cache and message broker and supports various data structures such as Strings, Hashes, Lists, Sets etc. Redis is written in ANSI C and works in most POSIX systems like Linux, *BSD and OS X without external dependencies. In this article we will learn how to clear Redis Cache.

Redis is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets. You can run atomic operations on these types like appending to a string; incrementing the value in a hash; pushing to a list; computing set intersection; or getting the member with highest ranking in a sorted set.

Read: How to configure Redis Object caching for WordPress

Why would you want to clear Redis Cache?

You might need to clear your Redis cache for one of the following reasons:

  • Your application is running slowly, so you want to check whether your Redis instance is the slow component.
  • You want to clear out a bunch of old data that's no longer relevant.
  • You're debugging a problem and want to see whether clearing Redis helps.

The redis-cli command is used to run commands against your Redis instance from your terminal. You need to have SSH access to your server.

If you don't know how to access server SSH, we've written a tutorial where we've explained how you can access terminal using Bitvise -> How To Easily Transfer Files Over SSH Using SCP

Clear Redis Cache With the redis-cli Command

The redis-cli is a great option if you have SSH access to your server. Follow these steps to use it:

  • Log into your server via SSH, as explained here
  • Use the redis-cli command to connect to Redis on your server, you can also login to remote Redis server using redis-cli

Alright, once you are logged into your Redis server, you can now run Redis commands, your terminal should look something like this

how to clear redis cache

Now in this terminal you can directly run Redis commands.

Syntax

redis-cli [options]

Since we are already logged into Redis server, we will omit "redis-cli" from some of our commands.

How to Clear Redis Cache from Command Line

We will now clear Redis cache using the flushall command

how to clear redis cache using flushall

As you can see we've directly used flushall command and didn't use the full form (redis-cli flushall) because we are already into Redis server.

This will clear everything from your Redis cache.

Delete Cache of specific Database

flushall command will remove everything from Redis cache, if you want to clear Redis Cache for certain database you first need to find out database number using:

redis-cli CONFIG GET databases
Note: We've omitted redis-cli from our command as we are already in Redis server as explained above

As you now have database numbers you can use command below to delete entries for certain database

redis-cli -n <database_number> flushdb

Replace <database_number> with your database number.

How to Clear Redis Cache from Command Line using async option

From Redis 4.0.0 onwards, keys can be cleared without affecting your server in any way. You can accomplish this by using the flushall command with the async parameter:

redis-cli flushall async

Similarly if you want to delete from certain database

redis-cli -n <database_number> flushdb async

Conclusion

In this article we've learned how to clear Redis cache using redis-cli. I've also personally noticed that while using Redis Caching with LiteSpeed Cache, it sometimes becomes really slow and when you disable object caching your site becomes normal.

In this case you can delete flush your Redis cache and re-enable it. However, if you still have any questions, feel free to ask below.

One comment on “How To Clear Redis Cache from Command Line (2 Easy Methods)”

Leave a Reply

Your email address will not be published. Required fields are marked *

chevron-down