I want to delete all keys. I want everything wiped out and give me a blank database.
Is there a way to do this in Redis client?
FLUSHALL Remove all keys from all databases
FLUSHDB Remove all keys from the current database
SCRIPT FLUSH Remove all the scripts from the script cache.
Use FLUSHALL ASYNC
if using (Redis 4.0.0 or greater) else FLUSHALL
.
https://redis.io/commands/flushall
Note: Everything before executing FLUSHALL ASYNC
will be evicted. The changes made during executing FLUSHALL ASYNC
will remain unaffected.
you can use following approach in python
def redis_clear_cache(self):
try:
redis_keys = self.redis_client.keys('*')
except Exception as e:
# print('redis_client.keys() raised exception => ' + str(e))
return 1
try:
if len(redis_keys) != 0:
self.redis_client.delete(*redis_keys)
except Exception as e:
# print('redis_client.delete() raised exception => ' + str(e))
return 1
# print("cleared cache")
return 0
If you are using Java then from the documentation, you can use any one of them based on your use case.
/**
* Remove all keys from all databases.
*
* @return String simple-string-reply
*/
String flushall();
/**
* Remove all keys asynchronously from all databases.
*
* @return String simple-string-reply
*/
String flushallAsync();
/**
* Remove all keys from the current database.
*
* @return String simple-string-reply
*/
String flushdb();
/**
* Remove all keys asynchronously from the current database.
*
* @return String simple-string-reply
*/
String flushdbAsync();
Code:
RedisAdvancedClusterCommands syncCommands = // get sync() or async() commands
syncCommands.flushdb();
Read more: https://github.com/lettuce-io/lettuce-core/wiki/Redis-Cluster
redis-cli -h <host> -p <port> flushall
It will remove all data from client connected(with host and port)
Heads up that FLUSHALL
may be overkill. FLUSHDB
is the one to flush a database only. FLUSHALL
will wipe out the entire server. As in every database on the server. Since the question was about flushing a database I think this is an important enough distinction to merit a separate answer.