redis_flushdbDeletes all keys in the currently selected database — equally destructive within its scope
FLUSHDB clears every key in the Redis database currently selected by the connection. While scoped to one DB index (0–15) rather than the entire instance like FLUSHALL, it is equally destructive within that scope. Most applications use DB 0 by default, making FLUSHDB functionally identical to FLUSHALL in a standard deployment.
Common Causes
- —Test teardown scripts using the wrong Redis connection or DB index
- —Development scripts run against the wrong environment
- —Cache reset tooling without DB scope validation
- —Admin routes that are not properly protected
How to Fix
- 1.Restrict FLUSHDB via Redis ACLs on the application user
- 2.Use a dedicated Redis DB index (e.g. DB 15) for test environments and flush only that
- 3.Replace with targeted key deletion using SCAN + UNLINK
- 4.Add environment and DB index assertions before any flush operation
Scoped but still destructive
FLUSHDB is often mistaken as "safer" than FLUSHALL because it only clears one database. In practice, if your application uses Redis DB 0 (the default), FLUSHDB wipes your entire application cache, session store, and any queues running in that DB.
Example
typescript
// BAD — clears all keys in the current DB (DB 0 by default)
await redis.flushdb();
// GOOD — isolated test DB with explicit guard
const TEST_DB_INDEX = 15;
const testRedis = new Redis({
host: process.env.REDIS_HOST,
port: 6379,
db: TEST_DB_INDEX, // explicit isolation
});
async function clearTestData() {
const dbIndex = await testRedis.client('INFO').then(/* parse current db */);
if (dbIndex !== TEST_DB_INDEX) {
throw new Error(`Expected DB ${TEST_DB_INDEX}, got ${dbIndex}`);
}
await testRedis.flushdb();
}
// GOOD — targeted key removal in production
async function clearUserCache(redis: Redis, userId: string) {
const pattern = `user:${userId}:*`;
let cursor = '0';
do {
const [next, keys] = await redis.scan(cursor, 'MATCH', pattern, 'COUNT', 50);
cursor = next;
if (keys.length > 0) await redis.unlink(...keys);
} while (cursor !== '0');
}DB isolation for tests
typescript
// jest.setup.ts
import Redis from 'ioredis';
const TEST_REDIS_DB = 15;
export const testRedis = new Redis({ db: TEST_REDIS_DB });
beforeEach(async () => {
// Verify we are on the right DB before clearing
const info = await testRedis.info('server');
if (!info.includes(`db${TEST_REDIS_DB}`)) {
throw new Error('Wrong Redis DB selected for tests');
}
await testRedis.flushdb();
});
afterAll(() => testRedis.quit());