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

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


当前回答

您可以使用FLUSHOLL从每个数据库中删除所有密钥。其中,FLUSHDB将删除当前数据库中的所有密钥。

其他回答

冲洗室,冲洗室从所有数据库中删除所有密钥

冲水阀从当前数据库中删除所有密钥

脚本刷新从脚本缓存中删除所有脚本。

如果使用(Redis 4.0.0或更高版本),则使用FLUSHOLL ASYNC,否则使用FLUSHALL。

https://redis.io/commands/flushall

注:执行FLUSHOLL ASYNC之前的所有内容都将被逐出。执行FLUSHOLL ASYNC期间所做的更改将不受影响。

使用redis cli:

FLUSHDB–从连接的当前数据库中删除所有密钥。FLUSHOLL–从所有数据库中删除所有密钥。

例如,在shell中:

redis-cli flushall

如果您使用的是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服务器后:service Redis server start--port 8000或Redis server。

使用redis cli-p 8000在不同的终端中作为客户端连接到服务器。

您可以使用

FLUSHDB-删除当前选定DB的所有键。此命令从未失败。此操作的时间复杂度为O(N),N是数据库中的密钥数。FLUSHOLL-删除所有现有数据库的所有键,而不仅仅是当前选定的键。此命令从未失败。此操作的时间复杂度为O(N),N是所有现有数据库中的密钥数。

检查ASYNC选项的文档。

如果您通过其python接口使用Redis,请使用以下两个函数实现相同的功能:

def flushall(self):
    "Delete all keys in all databases on the current host"
    return self.execute_command('FLUSHALL')

and

def flushdb(self):
    "Delete all keys in the current database"
    return self.execute_command('FLUSHDB')