Updated February 17, 2023
Introduction to Redis Delete All Keys
Redis delete all keys is a command used to delete all keys in the database. Command used to delete is FLUSHALL and FLUSHDB, these commands are executed on Redis CLI. Before getting into “delete all keys” command from Redis, let us get to know in brief about Redis and Redis CLI. Redis is a command line interface and Redis CLI is a terminal program that is used to send commands to the Redis server and receive replies. When caching is done in Redis, clearing the whole cache is useful when invalid. In this article, we will see how all keys are deleted in the particular database and across the database.
Overview of Redis Delete All Keys
Redis is an open-source key-value pair database. Redis stores data in memory which makes it faster in high-performance environments. Redis is used by smaller to larger applications that include Github, Twitter, StackOverflow, etc. Some cases arise, where the user needs to reset all of the databases in Redis. Redis FLUSHALL command lets the user delete all keys stored in the database in the Redis instance. Redis FLUSHDB command lets the user delete all keys stored in a particular database.
How to Use Redis Delete All Keys?
Similar to FLUSHALL, we also have another command i.e., FLUSHDB, used to delete keys in the database whereas the former is used to delete all keys in all databases.
These command operations are executed in the background as a thread using the ASYNC option. It means if deleting or the flush takes longer time, making command ASYNC stops from being blocked until it gets completed. And the ASYNC option is available for Redis 4.0.0 v onwards.
Before using Redis, it needs to get installed on the system.
Step 1: For installing Redis, we are using NodeJS and NPM, for a one-liner installation of redis-cli.
Code:
npm install -g redis-cli
Output:
Step 2: Then, use the below command for host and password setup for redis-cli.
Code:
rdcli -h your.redis.host -a yourredispassword -p 12345
Output:
Redis CLI has been installed, below are a few options or commands to check:
- rdcli – Help will show basic commands to start with.
- rdcli – Version shows the version installed on the system.
Step 3: Now connect to the Server host i.e., 127.0.0.1:6379, and execute the commands.
Example: FLUSHALL and FLUSHDB command.
Code:
redis-cli FLUSHALL ASYNC
redis-cli FLUSHDB -n 10 ASYNC
How to Delete All Keys in Redis?
redis-cli FLUSHALL [ASYNC | SYNC]: It is the command to delete all keys in all databases. This syntax or the command is available since v 1.0.0.
Time complexity of this command is O(n), n being the total number of keys in all the databases. This command will never fail and delete all the keys of the existing database and not only the currently selected database.
FLUSHALL synchronously flushes all keys in all databases by default. With Redis 6.2 version, if configuration lazyfree-lazy-user-flush is set to “yes”, default flush mode is set to Asynchronous mode.
One of the modifiers is used to present the flushing mode, i.e., ASYNC or SYNC.
- SYNC: Flushes or deletes all database keys synchronously.
- ASYNC: Flushes or deletes all database keys asynchronously.
redis-cli FLUSHDB [ASYNC | SYNC]: It is the command to delete all keys in the selected database. This command is available since v 1.0.0.
Time complexity of this command is O(n), n being the total number of keys in the selected database. This command will never fail and deletes all the keys of the selected database.
FLUSHALL synchronously flushes all keys in the database by default. With Redis 6.2 version, if configuration lazyfree-lazy-user-flush is set to “yes”, default flush mode is set to Asynchronous mode.
One of the modifiers is used to present the flushing mode, i.e., ASYNC or SYNC.
- SYNC: Flushes or deletes selected database keys synchronously.
- ASYNC: Flushes or deletes selected database keys asynchronously.
Asynchronous command FLUSHALL and FLUSHDB deletes keys that are present when the command is invoked and the keys that are created at the time of asynchronous flush are not affected.
ASYNC flush mode is added from Redis v 4.0.0 and the SYNC flush mode is added from Redis v 6.2.0.
Redis Delete All Keys Clear Cache
With Redis cli, whole cache data can be cleared when invalid. Easier way to clear the cache is to use the redis-cli command, as databases are stored individually, using redis-cli allows the user to delete all keys from all databases or from a particular database.
Code:
redis-cli [database number][option]
- database number: User can specify which database is to be cleared.
- option: User can choose either clearing all databases or one database.
As the default host and port are 127.0.0.1:6379, it can be overridden with the below command.
Code:
redis-cli -h [hostname] -p [portname] command
redis-cli -h 191.169.5.400 -p 8000 FLUSHDB
redis-cli -h 10.4.0.0 FLUSHALL
Clearing Redis cache may have a few reasons:
- User wants to clear a bunch of older data that is no longer required or relevant.
- If the application is running slowly and the user wants to check if the instance is slow.
- User debugging an issue and wants to check if clearing the cache can help to resolve it.
Conclusion
With this, we conclude the topic “Redis delete all keys”, and have seen what it mean and how is it executed on the command prompt. We have gone through an overview and seen how it is used in Redis and the deletion with commands for FLUSHDB and FLUSHALL. Also have put down a few points on redis delete all keys clear the cache, which can help the user if the application slows down, in process of debugging, or in cases where an old bunch of data is not relevant.
Recommended Articles
This is a guide to Redis Delete All Keys. Here we discuss the introduction, use, how to delete all keys in redis? and clear cache. You may also have a look at the following articles to learn more –