我想删除所有密钥。我要把一切都删掉,给我一个空白的数据库。

有没有办法在Redis客户端中做到这一点?


当前回答

如果您使用的是Java,那么从文档中可以根据您的用例使用其中的任何一个。

/**
 * 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();

代码:

RedisAdvancedClusterCommands syncCommands = // get sync() or async() commands 
syncCommands.flushdb();

阅读更多信息:https://github.com/lettuce-io/lettuce-core/wiki/Redis-Cluster

其他回答

停止Redis实例。删除RDB文件。启动Redis实例。

警惕FLUSHOLL可能过度使用。FLUSHDB仅用于刷新数据库。FLUSHOLL将清除整个服务器。就像服务器上的每个数据库一样。由于问题是关于刷新数据库,我认为这是一个足够重要的区别,值得单独回答。

我认为有时会停止redis服务器并删除rdb、aof文件确保没有可以重新加载的数据。然后启动redis服务器,现在它是空的。

您可以在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

你可以在航站楼使用flushhall

redis-cli> flushall