Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Delete Everything in Redis

$
0
0

1. Overview

When caching in Redis, it can be useful to clear the whole cache when it becomes invalid.

In this short tutorial, we'll learn how to delete all the keys present in Redis, both in specific databases, and across all databases.

First, we'll look at the command line. Then, we'll see how to accomplish the same thing using the APIs and the Java client.

2. Redis Commands

Let's start with the Redis commands to delete everything.

There are two major commands to delete the keys present in Redis: FLUSHDB and FLUSHALL. We can use the Redis CLI to execute these commands.

The FLUSHDB command deletes the keys in a database. And the FLUSHALL command deletes all keys in all databases.

We can execute these operations in a background thread using the ASYNC option. This is useful if the flush takes a long time, as making the command ASYNC stops it from blocking until its complete.

We should note that the ASYNC option is available from Redis 4.0.0.

3. Delete Using the Java Client

Now let's try to delete the keys using the Java client.

We'll make use of an embedded Redis server and see how to clear keys.

3.1. Clearing a Single Database

Let's put some data into a database, and then clear it. We'll then persist and flush the database using the flushdb method.

We interact with our server through the RedisCallback interface:

ValueOperations<String, String> simpleValues = flushRedisTemplate.opsForValue();
String key = "key";
String value = "value";
simpleValues.set(key, value);
assertThat(simpleValues.get(key)).isEqualTo(value);

flushRedisTemplate.execute(new RedisCallback<Void>() {
    @Override
    public Void doInRedis(RedisConnection connection) throws DataAccessException {
        connection.flushDb();
        return null;
    }
}

assertThat(simpleValues.get(key)).isNull();

First, we used set to add a key to our database.

Then we used the flushRedisTemplate and called flushdb within the callback to clear the database.

Finally, after clearing the database, attempting to retrieve the value for the key returned null.

3.2. Clearing All Databases

In order to clear all the databases, we use the above technique with the flushAll method:

public Void doInRedis(RedisConnection connection) throws DataAccessException {
    connection.flushAll();
    return null;
}

4. Time Complexity

Redis is a fast data store which scales well. However, flush operations can take longer when there is more data.

The time complexity of the FLUSHDB operation is O(N), where N is the number of keys in the database. If we use the FLUSHALL command, the time complexity is again O(N), but here, N is the number of keys in all the databases.

5. Conclusion

In this tutorial, we learned how to delete everything in Redis using the command line and the Java client.

As always, the source code for this article is available over on GitHub.


Viewing all articles
Browse latest Browse all 4535

Trending Articles