Updated February 18, 2023
Introduction to Redis FLUSHDB
In simple, the Redis FLUSHDB option is implied to delete every key in the selected database. The FLUSHDB command never fails in any case and it is available since version 1.0. The time complexity is defined as O (N) where N signifies the count of keys in the chosen database. It applies to all categories like slow, dangerous, writes, and keyspace.
Key Takeaways
- Redis 4.0 and after that version can able to delete all keys in the background as a single thread without server blocking.
- Async option is used for FLUSHDB and FLUSHALL in the entire dataset.
- Due to security reasons, the user wants to hide the authentication password from others in the Linux and not expose it.
- The Redis FLUSHDB command removes every key in the chosen database and once executed, this command never fails.
What is Redis FLUSHDB?
Redis FLUSHDB is used to remove all the present keys from the selected database. By default, to flush the keys synchronously from the database, use the FLUSHDB command. From Redis 6.2, fix the lazy free config settings to yes, to change the mode from synchronous to asynchronous. It can be used with modifiers to work according to the flushing mode given.
Few modifiers are:
- ASYNC is used to flush the DB in an asynchronous way.
- SYNC is used to flush the DB in a synchronous way.
Keys generated at asynchronous flush will stay unaffected, and when the asynchronous FLUSHDB command is invoked, it deletes only the keys that are present. In this blog, the user can learn to remove all the keys present in the database and can clear the cache with a simple method.
Redis FLUSHDB Empty
Redis is an open-source, free, key-value data store that saves the data in the memory of the system instead of the disk. This is the unique feature that enables Redis to be cognitive and extremely compute compared to other relational database. It is an important feature that has effective performance in a minimum latency environment.
Redis-CLI FLUSHDB command allows the user to remove keys from the pointed database.
If the Redis is executed on the remote machine, the user can give the address of the host machine using –h as below:
Redis cli –h < IP of host machine > FLUSHDB
It is advised to use the FLUSHDB command without passing any argument as it removes every key in the pointed database. If it is not given, the default database will start from index 0.
If the user wants to target the specific database index, he can use –n followed by an index of the database.
Syntax:
Redis cli –n [index of the db] FLUSHDB
If the user gives 11, it clears all the keys at index 11.
Redis cli –n 11 FLUSHDB
Since Redis 4.0, enables the user to execute flush operations on the segregated thread, it also restricts the flush operation from restricting the server till it gets completed.
To execute the asynchronous flush operation, follow the below command:
Redis CLI FLUSHDB async
Redis FLUSHDB Server Command
To delete everything from the Redis, two major commands are FLUSHALL and FLUSHDB. It will be easy if those commands are executed in Redis CLI.
The FLUSHDB command executes the delete operation in keys present in the database. The FLUSHALL commands remove all the keys present in the database.
The command executes the operation in the backend thread with the async option. If it takes a long time to flush by using the async option, it gets blocked till gets completed. The user checks on the option that is present after Redis 4.0
Redis FLUSHDB Key and Scan
The iterator based on the scan signifies every call on commands and the server executes the updated cursor values which the user applies to the argument in the successor call.
Syntax:
SCAN [ pattern match ] [count]
The Redis scan command is applied to increase the iteration over the element collection. Every iteration starts when the pointer is fixed to 0 and stops when the pointer or cursor gives the output from the server as 0. The option is available from the version Redis 2.8.0
The new cursor has the first value, which should be used in the next call and the second value comprises an array of the element. In the second call, if the value is returned to 0, and waits till the scan returns 0, which is the full iteration.
Redis FLUSHDB Syntax to Cache
The easy method to clear the cache is by using the Redis CLI utility. In the Redis command line interface, the user can delete all the keys present in a database available in the cluster of Redis.
Syntax:
Redis cli <number of the database> < option >
The database number enables the user to look at the target database that needs to be erased. Option – is given for clear operation.
It should be noted that the hostname of the server is 127.0.0.1 and the default port is 6379 give a password and socket when connecting to the server by using the syntax below.
Redis cli -h {hostname of the machine_IP} -p {port number} -s {socket} -a {password} FLUSHDB
Redis cli -h {hostname of the machine_IP} -p {port number} -s {socket} -a {password} FLUSHALL
FAQ
Given below are the FAQs mentioned:
Q1. How to Authenticate the Redis database?
Answer:
The user can simply REDIS CLI _ AUTH as an environment variable to surpass the password in a secure method.
Syntax:
REDIS CLI_AUTH='mypassword' Redis CLI 10.8.5.4 -p 6379 FLUSHDB
Q2. How to Clear the Redis cache?
Answer:
The default hostname is 127.0.0.1 and the number of Redis ports is 6379.
Command:
$ Redis cli -h {hostnmachine IP} -p {portnumber}
Q3. How to Delete keys from a particular DB?
Answer:
Code:
Redis cli –n [index of the db] FLUSHDB
Conclusion
Hence, these are the few basic operations using the FLUSHDB command in Redis. It is a user-friendly and reliable method to work effectively on the Redis database as it deals with higher dimensional data.
Recommended Articles
This is a guide to Redis FLUSHDB. Here we discuss the introduction, server command, syntax few basic operations using the FLUSHDB command. You may also have a look at the following articles to learn more –