How to Perform the Redis Flush All Command
Introduction
This tutorial will explain how to perform the Redis Flush functions, specifically the Redis Flush All and Flush DB commands. The Redis Command Line Interface utility provides a simple command, used through the CLI, that will flush, or delete, all of the Redis keys on every database on a server. The FLUSH ALL command should not be confused with the FLUSH DB command, the latter of which is designed to only delete the keys of a singe specified database. However, the FLUSH ALL command will delete all of the keys from all of the databases stored on Redis server.
Prerequisites
Following are the prerequisites required for executing the Redis Flush All and FlushDB commands:
- A Redis database must be properly installed, configured and up and running on the local system. Obtain the currently installed version of the Redis instance by executing the
redis-server –version
command.
The Redis-Cli Flush All
The Redis FLUSH ALL command is designed to delete all of the keys in all of the database that are stored on a given server. Execute the following command, in the command line interface, to quickly erase all of the keys stored on all of the databases:
1 | redis-cli FLUSHALL |
Execute the following FLUSHDB command in the command line interface to delete all of the existing keys in only the current database:
1 | redis-cli FLUSHDB |
Create a Key in Redis Database
Execute the following command, in the terminal, to run the interactive Redis command-line interface:
1 | redis-cli |
Database statements can be executed in the Redis command line interface using the default port of 6379
, as shown here:
1 | 127.0.0.1:6379> |
NOTE: To switch into a database in Redis, the number of the targeted index must be selected. This number must begin with the default index of 0 for new connections to the Redis command line interface, up to an index number of 15, consisting of a total of 16 databases in Redis.
The basic command to create a string in Redis is SET
. Execute the command shown in the following example to create a new key with a value in Redis:
1 2 | 127.0.0.1:6379> SET some_key "some_value" OK |
NOTE: Execute the GET
command in Redis to retrieve the output of the new key provided in the above example.
Keys in Redis
The KEYS command is used to retrieve the key name that matches the pattern in the Redis database. Execute the KEYS command as follows:
1 | KEYS [pattern] |
Following is a working example of using the KEYS command:
1 2 | 127.0.0.1:6379> KEYS count 1) "count" |
NOTE: The asterisk ( *
) symbol can be used to include all of the keys in Redis.
To display the Redis command line instance of the keyspace parameter, execute the following INFO
command in the currently selected database:
1 | 127.0.0.1:6379> INFO keyspace |
The output should resemble the following:
1 2 3 4 | # Keyspace db0:keys=76,expires=0,avg_ttl=0 db1:keys=1,expires=0,avg_ttl=0 db2:keys=1,expires=0,avg_ttl=0 |
Notice that three databases are used in the above example, with the first one having 76 keys stored in it without a set expiration.
Use the Redis to Flush All Keys in the Database
Redis can also delete keys in a separate thread, without blocking the server in the background. As shown in the following example, a Redis cache or database can be flushed to remove all of the stored keys:
1 2 | 127.0.0.1:6379> FLUSHALL OK |
Below is a final example of how to flush the keys in a specific database. First, select the desired index, as shown on line one, and then execute the FLUSHDB
command:
1 2 3 4 | 127.0.0.1:6379> SELECT 1 OK 127.0.0.1:6379[1]> FLUSHDB OK |
Conclusion
This tutorial covered how to perform the Redis Flush All and Flush DB commands. The tutorial explained how to use the Redis command line interface to execute the Flush functions and how to create keys in Redis to retrieve the key name in the database. The article then explained how to use Redis to flush keys in a database and provided working examples. When using these two commands, it is important to remember that the Redis FLUSH ALL command is used to delete all keys from all databases on a Redis server and the FLUSH DB command is used to delete all keys from only the currently selected or specified database. Jump to top
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started