How to Delete All Data in Redis

Data Tutorial

Redis has grown to become one of the most popular NoSQL database systems (not to mention cache systems) in use today. Perhaps the biggest advantage Redis has over other NoSQL systems is that runs almost entirely in memory.

This means that, unlike the disk-based storage of a system like MongoDB, Redis is extremely fast. The downside of running in memory is, of course, that as your database size increases, so to does your memory usage. Redis’ FAQ has more details on the actual memory use required of a few simple examples, but for developers and database administrators, the pros and cons inherent in Redis are worth considering.

Part of the sheer power of Redis being a NoSQL and in-memory system is that some tasks that would require multiple, complex queries in relational database systems can be accomplished very easily in Redis.

One such capability, which we’ll explore here, is deleting everything in your entire database or even all databases!

It should go without saying: proceed with caution.

Starting Redis

For most installations, Redis will be automatically launched with a startup or initialization script, but if you need to manually start the Redis server, this can be accomplished easily with the redis-server command from your shell prompt.

$ redis-server
[25694] 05 Feb 23:34:18.769 * Max number of open files set to 10032
[25694] 05 Feb 23:34:18.772 # Server started, Redis version 2.8.4
[25694] 05 Feb 23:34:18.777 * DB loaded from disk: 0.005 seconds
[25694] 05 Feb 23:34:18.777 * The server is now ready to accept connections on port 6379

If successful, you’ll see an output from Redis similar to the above, indicating the server is running and which port it is attached to.

Accessing the Redis Command Line Interface

All Redis installations come with the the Redis Command Line Interface, which can be accessed by executing the redis-cli command.

$ redis-cli
127.0.0.1:6379>

If Redis is running and you were able to connect, you’ll be viewing the redis-cli prompt with the host and port specified, as seen above.

Deleting a Single Database

If your Redis instance is running multiple databases, these databases will be differentiated from one another by their unique index number.

You may connect to a different database by entering the select # command:

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]>

Notice that the redis-cli prompt now indicates you are connected to database 1.

To destroy a specific database, first select it as above, then issue the FLUSHDB command:

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> FLUSHDB
OK

Deleting ALL Databases

If you really hate your Redis instance and wish to destroy everything in the entire system, use the FLUSHALL command:

127.0.0.1:6379> FLUSHALL
OK

Terrifyingly simple, but that is how you can quickly (and too easily) delete everything in Redis.