FLUSHALL
FLUSHALL [ASYNC | SYNC]
- Available since:
- Redis Open Source 1.0.0
- Time complexity:
- O(N) where N is the total number of keys in all databases
- ACL categories:
-
@keyspace,@write,@slow,@dangerous, - Compatibility:
- Redis Software and Redis Cloud compatibility
Note:
This command's behavior varies in clustered Redis environments. See the multi-key operations page for more information.Delete all the keys of all the existing databases, not just the currently selected one. This command never fails.
By default, FLUSHALL will synchronously flush all the databases.
Starting with Redis 6.2, setting the lazyfree-lazy-user-flush configuration directive to yes changes the default flush mode to asynchronous.
Full delete: Delete all keys from all databases using FLUSHALL (dangerous operation, supports ASYNC/SYNC modes, clears RDB file)
FLUSHALL SYNC
res1 = r.flushall(asynchronous=False)
print(res1) # >>> True
res2 = r.keys()
print(res2) # >>> []
import redis
r = redis.Redis(decode_responses=True)
res1 = r.flushall(asynchronous=False)
print(res1) # >>> True
res2 = r.keys()
print(res2) # >>> []
res3 = r.info()
print(res3)
# >>> {'redis_version': '7.4.0', 'redis_git_sha1': 'c9d29f6a',...}
const res1 = await client.flushAll('SYNC'); // or ASYNC
console.log(res1); // OK
const res2 = await client.keys('*');
console.log(res2); // []
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
const res1 = await client.flushAll('SYNC'); // or ASYNC
console.log(res1); // OK
const res2 = await client.keys('*');
console.log(res2); // []
const res3 = await client.info();
console.log(res3)
// # Server
// redis_version:7.4.0
// redis_git_sha1:c9d29f6a
// redis_git_dirty:0
// redis_build_id:4c367a16e3f9616
// redis_mode:standalone
// ...
await client.close();
const res1 = await redis.flushall(); // or flushall('ASYNC')
console.log(res1); // >>> OK
const res2 = await redis.keys('*');
console.log(res2); // >>> []
import assert from 'node:assert';
import { Redis } from 'ioredis';
const redis = new Redis();
const res1 = await redis.flushall(); // or flushall('ASYNC')
console.log(res1); // >>> OK
const res2 = await redis.keys('*');
console.log(res2); // >>> []
const res3 = await redis.info();
console.log(res3);
// >>> # Server
// >>> redis_version:7.4.0
// >>> ...
redis.disconnect();
String flushAllResult1 = jedis.flushAll();
System.out.println(flushAllResult1); // >>> OK
Set<String> flushAllResult2 = jedis.keys("*");
System.out.println(flushAllResult2); // >>> []
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.RedisClient;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CmdsServerMgmtExample {
public void run() {
RedisClient jedis = RedisClient.create("redis://localhost:6379");
String flushAllResult1 = jedis.flushAll();
System.out.println(flushAllResult1); // >>> OK
Set<String> flushAllResult2 = jedis.keys("*");
System.out.println(flushAllResult2); // >>> []
// Note: you must use the `Jedis` class to access the `info`
// command rather than `RedisClient`.
Jedis jedis2 = new Jedis("redis://localhost:6379");
String infoResult = jedis2.info();
// Check the first 8 characters of the result (the full `info` string
// is much longer than this).
System.out.println(infoResult.substring(0, 8)); // >>> # Server
jedis2.close();
jedis.close();
}
}
CompletableFuture<Void> flushallExample = asyncCommands.flushall()
.thenCompose(res1 -> {
System.out.println(res1); // >>> OK
return asyncCommands.keys("*");
}).thenAccept(res2 -> {
System.out.println(res2); // >>> []
}).toCompletableFuture();
package io.redis.examples.async;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.StatefulRedisConnection;
import java.util.concurrent.CompletableFuture;
public class CmdsServerMgmtExample {
public void run() {
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
RedisAsyncCommands<String, String> asyncCommands = connection.async();
CompletableFuture<Void> flushallExample = asyncCommands.flushall()
.thenCompose(res1 -> {
System.out.println(res1); // >>> OK
return asyncCommands.keys("*");
}).thenAccept(res2 -> {
System.out.println(res2); // >>> []
}).toCompletableFuture();
flushallExample.join();
CompletableFuture<Void> infoExample = asyncCommands.info()
.thenAccept(res3 -> {
System.out.println(res3);
// >>> # Server
// >>> redis_version:7.4.0
// >>> ...
}).toCompletableFuture();
infoExample.join();
} finally {
redisClient.shutdown();
}
}
}
Mono<Void> flushallExample = reactiveCommands.flushall()
.doOnNext(res1 -> {
System.out.println(res1); // >>> OK
})
.then(reactiveCommands.keys("*").collectList())
.doOnNext(res2 -> {
System.out.println(res2); // >>> []
})
.then();
package io.redis.examples.reactive;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.reactive.RedisReactiveCommands;
import io.lettuce.core.api.StatefulRedisConnection;
import reactor.core.publisher.Mono;
public class CmdsServerMgmtExample {
public void run() {
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
RedisReactiveCommands<String, String> reactiveCommands = connection.reactive();
Mono<Void> flushallExample = reactiveCommands.flushall()
.doOnNext(res1 -> {
System.out.println(res1); // >>> OK
})
.then(reactiveCommands.keys("*").collectList())
.doOnNext(res2 -> {
System.out.println(res2); // >>> []
})
.then();
flushallExample.block();
Mono<Void> infoExample = reactiveCommands.info()
.doOnNext(res3 -> {
System.out.println(res3);
// >>> # Server
// >>> redis_version:7.4.0
// >>> ...
})
.then();
infoExample.block();
} finally {
redisClient.shutdown();
}
}
}
flushAllResult1, err := rdb.FlushAll(ctx).Result()
if err != nil {
panic(err)
}
fmt.Println(flushAllResult1) // >>> OK
flushAllResult2, err := rdb.Keys(ctx, "*").Result()
if err != nil {
panic(err)
}
fmt.Println(flushAllResult2) // >>> []
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_cmd_flushall() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
flushAllResult1, err := rdb.FlushAll(ctx).Result()
if err != nil {
panic(err)
}
fmt.Println(flushAllResult1) // >>> OK
flushAllResult2, err := rdb.Keys(ctx, "*").Result()
if err != nil {
panic(err)
}
fmt.Println(flushAllResult2) // >>> []
}
func ExampleClient_cmd_info() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
})
infoResult, err := rdb.Info(ctx).Result()
if err != nil {
panic(err)
}
// Check the first 8 characters (the full info string contains
// much more text than this).
fmt.Println(infoResult[:8]) // >>> # Server
}
server.FlushAllDatabases();
var res2 = server.Keys(pattern: "*").ToArray();
Console.WriteLine(res2.Length); // >>> 0
using System;
using System.Linq;
using StackExchange.Redis;
public class CmdsServerMgmt
{
public void Run()
{
// FLUSHALL and INFO are server-admin commands, so connect with
// allowAdmin=true and issue them through the IServer object.
var muxer = ConnectionMultiplexer.Connect("localhost:6379,allowAdmin=true");
var server = muxer.GetServer("localhost:6379");
server.FlushAllDatabases();
var res2 = server.Keys(pattern: "*").ToArray();
Console.WriteLine(res2.Length); // >>> 0
var res3 = server.Info();
Console.WriteLine(res3.Length > 0); // >>> True
}
}
$res1 = $r->flushall();
echo $res1 . PHP_EOL; // >>> OK
$res2 = $r->keys('*');
echo json_encode($res2) . PHP_EOL; // >>> []
<?php
use Predis\Client as PredisClient;
class CmdsServerMgmtTest
{
public function testCmdsServerMgmt() {
$r = new PredisClient([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
'password' => '',
'database' => 0,
]);
$res1 = $r->flushall();
echo $res1 . PHP_EOL; // >>> OK
$res2 = $r->keys('*');
echo json_encode($res2) . PHP_EOL; // >>> []
$res3 = $r->info();
echo $res3['Server']['redis_version'] . PHP_EOL;
// >>> 7.4.0
}
}
res1 = r.flushall # or r.flushall(async: true)
puts res1 # >>> OK
res2 = r.keys('*')
puts res2.inspect # >>> []
require 'redis'
r = Redis.new
res1 = r.flushall # or r.flushall(async: true)
puts res1 # >>> OK
res2 = r.keys('*')
puts res2.inspect # >>> []
res3 = r.info
puts res3['redis_version']
# >>> 7.4.0
let _: () = r.flushall().expect("Failed to flushall");
match r.keys("*") {
Ok(res2) => {
let res2: Vec<String> = res2;
println!("{res2:?}"); // >>> []
}
Err(e) => println!("Error getting keys: {e}"),
}
mod cmds_servermgmt_tests {
use redis::Commands;
fn run() {
let mut r = match redis::Client::open("redis://127.0.0.1") {
Ok(client) => match client.get_connection() {
Ok(conn) => conn,
Err(e) => {
println!("Failed to connect to Redis: {e}");
return;
}
},
Err(e) => {
println!("Failed to create Redis client: {e}");
return;
}
};
let _: () = r.flushall().expect("Failed to flushall");
match r.keys("*") {
Ok(res2) => {
let res2: Vec<String> = res2;
println!("{res2:?}"); // >>> []
}
Err(e) => println!("Error getting keys: {e}"),
}
match redis::cmd("INFO").query::<String>(&mut r) {
Ok(res3) => {
println!("{res3}");
// >>> # Server
// >>> redis_version:7.4.0
// >>> ...
}
Err(e) => println!("Error getting info: {e}"),
}
}
}
let _: () = r.flushall().await.expect("Failed to flushall");
match r.keys("*").await {
Ok(res2) => {
let res2: Vec<String> = res2;
println!("{res2:?}"); // >>> []
}
Err(e) => println!("Error getting keys: {e}"),
}
mod cmds_servermgmt_tests {
use redis::AsyncCommands;
async fn run() {
let mut r = match redis::Client::open("redis://127.0.0.1") {
Ok(client) => match client.get_multiplexed_async_connection().await {
Ok(conn) => conn,
Err(e) => {
println!("Failed to connect to Redis: {e}");
return;
}
},
Err(e) => {
println!("Failed to create Redis client: {e}");
return;
}
};
let _: () = r.flushall().await.expect("Failed to flushall");
match r.keys("*").await {
Ok(res2) => {
let res2: Vec<String> = res2;
println!("{res2:?}"); // >>> []
}
Err(e) => println!("Error getting keys: {e}"),
}
match redis::cmd("INFO").query_async::<String>(&mut r).await {
Ok(res3) => {
println!("{res3}");
// >>> # Server
// >>> redis_version:7.4.0
// >>> ...
}
Err(e) => println!("Error getting info: {e}"),
}
}
}
Optional arguments
ASYNC | SYNC
Flush asynchronously (ASYNC) or synchronously (SYNC). The default is set by the lazyfree-lazy-user-flush configuration directive.
Details
- An asynchronous
FLUSHALLcommand only deletes keys that were present at the time the command was invoked. Keys created during an asynchronous flush will be unaffected. - This command does not delete functions.
- Other than emptying all databases (similar to
FLUSHDB), this command clears the RDB persistence file, aborts any snapshot that is in progress, and, if thesaveconfig is enabled, saves an empty RDB file.
Redis Software and Redis Cloud compatibility
| Redis Software |
Redis Cloud |
Notes |
|---|---|---|
| ✅ Standard |
✅ Standard |
*Can use the Active-Active flush API request. |
Return information
Simple string reply:
OK.
History
- Starting with Redis version 4.0.0: Added the
ASYNCflushing mode modifier. - Starting with Redis version 6.2.0: Added the
SYNCflushing mode modifier. The default flush behavior is now configurable using the lazyfree-lazy-user-flush configuration directive.