Shaheen Hyder
Shaheen Hyder

Reputation: 360

How to delete all data from Redis using RedisTemplate in java

I am using Lettuce as Redis client for my Java Spring project. I am doing several operations with RedisTemplate. I'm not able to delete all data from Redis using RedisTemplate.

I tried

redisTemplate.delete("*")

However, this is not making any change.

Upvotes: 3

Views: 7966

Answers (3)

sazzad
sazzad

Reputation: 6267

Try:

redisTemplate.getConnectionFactory().getConnection().flushAll();

Update: The above method is deprecated since spring-data-redis 3.x. In that case, check:

redisTemplate.getConnectionFactory().getConnection().serverCommands().flushAll();

Upvotes: 10

Aniruddha Mukherjee
Aniruddha Mukherjee

Reputation: 1

redisTemplate.getConnectionFactory().getConnection().flushAll();

This thing is also deprecated.

You can try this one:

RedisConnection redisConnection = this.redisTemplate.getConnectionFactory().getConnection();

RedisSerializer<String> redisSerializer = this.redisTemplate.getKeySerializer();

DefaultStringRedisConnection defaultStringRedisConnection = new DefaultStringRedisConnection(redisConnection, redisSerializer);

defaultStringRedisConnection.flushAll();

Upvotes: 0

Jonathan
Jonathan

Reputation: 883

In case you are using kotlin and reactive connection within a suspendable function, the code is:

suspend fun flushCache(): String {
   return redisTemplate.connectionFactory.reactiveConnection.serverCommands().flushDb().awaitSingle()
}

Upvotes: 3

Related Questions