Remove All Keys from Redis in PHP
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Remove All Keys from Redis in PHP: A Developer's Guide
I remember the frustration. I found that the most straightforward way to wipe out an entire Redis database was using the command-line utility: redis-cli flushdb. As a developer working primarily in the PHP ecosystem, I spent valuable time searching for the equivalent operation within PHP, hoping for a simple function call. Since I couldn't find a direct, one-line method, I decided to share the reliable programmatic approach.
If you are managing session data, temporary caches, or running destructive environment tests in your application backend, knowing how to clear Redis efficiently via PHP is a massive time-saver. This guide will walk you through the best methods for removing all keys from your Redis instance using popular PHP libraries.
Understanding the Approach: Direct vs. Iterative Deletion
When interacting with Redis from an external client like PHP, there are two primary ways to achieve a full key removal: executing a single server command or manually iterating over the keys.
Method 1: The Atomic Flush (The Fastest Way)
The most efficient way to delete every key in a database is by using the native Redis command FLUSHALL. This command tells the Redis server itself to clear all keys across all databases. When executed via a client library, this operation is atomic and extremely fast, as it delegates the entire workload directly to the Redis server.
Method 2: The Iterative Deletion (The Programmatic Way)
If you need more granular control, or if FLUSHALL permissions are restricted in your environment, you must iterate over all existing keys and delete them one by one using the DEL command. This method is useful for debugging or selectively clearing specific key patterns.
For iteration, we use commands like KEYS (for small databases) or SCAN (the recommended approach for large production systems).
Implementation with PHP and Predis
We will demonstrate both methods using the widely respected Predis library to connect to the Redis instance.
Example 1: Using FLUSHALL (The Recommended Approach)
This method is preferred for bulk deletion because it minimizes network latency compared to fetching every key individually.
<?php
require 'vendor/autoload.php';
use Predis\Client;
// Configuration details
$host = '127.0.0.1';
$port = 6379;
$password = null; // Set if authentication is required
try {
// Initialize the Redis client
$redis = new Client($host . ':' . $port);
echo "Attempting to flush all keys using FLUSHALL...\n";
// Execute the FLUSHALL command atomically
$result = $redis->flushall();
if ($result === true) {
echo "Success! All keys have been flushed from Redis.\n";
} else {
echo "Warning: FLUSHALL executed, but result was not explicitly success.\n";
}
} catch (Exception $e) {
echo "An error occurred while connecting or executing the command: " . $e->getMessage() . "\n";
}
Example 2: Iterative Deletion using SCAN (For Granular Control)
If you need to verify keys before deletion, or if you are operating in a highly secured environment, iterating via SCAN is safer than KEYS.
<?php
require 'vendor/autoload.php';
use Predis\Client;
$redis = new Client('127.0.0.1:6379');
echo "Starting iterative key scan and deletion...\n";
// Use SCAN to iterate through all keys safely (recommended for large datasets)
$cursor = '0';
$keysDeleted = 0;
do {
// Scan returns the next cursor and the keys found in that iteration
$cursor = $redis->scan($cursor, 'MATCH', '*');
$keys = $redis->scan($cursor); // Fetch actual keys using the cursor
if (!empty($keys)) {
// Delete the keys found in this batch
$deletedCount = $redis->del($keys);
echo "Deleted $deletedCount keys in this batch.\n";
$keysDeleted += $deletedCount;
}
} while ($cursor !== '0');
echo "\nProcess complete. Total keys deleted: " . $keysDeleted . "\n";
Best Practices for Production Environments
When dealing with data persistence and caching, remember that efficiency is crucial. For high-throughput applications, favor the atomic FLUSHALL command (Method 1). However, always ensure you have appropriate permissions on your Redis instance.
Furthermore, when designing systems that rely heavily on fast data access, understanding the limitations of in-memory stores versus persistent storage is key. Just as building robust backend services requires careful architectural planning—much like structuring a large application using frameworks like Laravel—managing your cache effectively requires choosing the right tools and commands for the job. Always prioritize performance and security when managing sensitive data stored in memory.
Conclusion
Whether you choose the simple, atomic FLUSHALL command or the more complex, safer iterative SCAN approach, PHP provides powerful tools to manage your Redis keys efficiently. By leveraging robust libraries like Predis, developers can ensure their backend operations are both powerful and highly optimized. Use Method 1 for speed in bulk clearing, and Method 2 for detailed auditing when necessary.