How To Clear Redis Cache?
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 the highest ranking in a sorted set.
Table of Contents
Why would you want to clear Redis Cache?
You might need to clear your Redis cache for one of the following reasons:
Find Out What's New: Click to Our Latest Blog
- 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 the terminal using Bitvise -> How To Easily Transfer Files Over SSH Using SCP
Enhance Your CyerPanel Experience Today!
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 log in 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
Now in this terminal, you can directly run Redis commands.
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
As you can see we've directly used flushall command and didn't use the full form (redis-cli flushall) because we are already on 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 the database number using:
redis-cli CONFIG GET databases
As you now have database numbers you can use the 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 a certain database
redis-cli -n <database_number> flushdb async
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 and flush your Redis cache and re-enable it. However, if you still have any questions, feel free to ask below.
Related Content: Snapshot vs Backup