Skip to main content

Description

Delete all the keys in the currently selected database. This command never fails.
This is a destructive operation that cannot be undone. Use with extreme caution, especially in production environments.

Method Signature

flushdb(opts?: { async?: boolean }): Promise<"OK">

Parameters

opts
object
Optional configuration object:

Return Value

result
'OK'
Always returns "OK"

Examples

Flush database synchronously

await redis.set("key1", "value1");
await redis.set("key2", "value2");

const result = await redis.flushdb();
console.log(result); // "OK"

const size = await redis.dbsize();
console.log(size); // 0

Flush database asynchronously

const result = await redis.flushdb({ async: true });
console.log(result); // "OK"

// Deletion happens in background
// Database may not be empty immediately

Clear test data

import { beforeEach } from "vitest";

beforeEach(async () => {
  // Clean database before each test
  await redis.flushdb();
});

Safe flush with confirmation

const confirmFlush = process.env.CONFIRM_FLUSH === "true";

if (confirmFlush) {
  await redis.flushdb();
  console.log("Database flushed");
} else {
  console.log("Flush cancelled - set CONFIRM_FLUSH=true to proceed");
}

Check size before and after

const before = await redis.dbsize();
console.log(`Keys before flush: ${before}`);

await redis.flushdb();

const after = await redis.dbsize();
console.log(`Keys after flush: ${after}`);

Async vs Sync

Synchronous (default)

  • Blocks the server until all keys are deleted
  • Suitable for small databases
  • Guarantees database is empty when command returns

Asynchronous

  • Returns immediately
  • Deletion happens in background
  • Suitable for large databases
  • Doesn’t block other operations

See Also

  • del - Delete specific keys
  • dbsize - Get number of keys