Skip to content

Memcached - Clear Data

The flush_all command invalidates all existing items in the Memcached server immediately or after a specified delay. It is a powerful but destructive command that should be used with extreme caution, especially in a production environment.

Executing flush_all will wipe the entire cache for that Memcached instance. This can cause a “thundering herd” problem, where a flood of requests suddenly hits your backend database or services, potentially overwhelming them. This command is typically used in development or testing environments to reset the cache state.

The basic syntax for the flush_all command is:

flush_all [delay] [noreply]

The optional delay parameter specifies a number of seconds after which all items will be invalidated. The server will always respond with OK.

In this example, we set a couple of keys, verify they exist, then flush the cache and confirm they are gone.

# Set some data
set key1 0 900 5
hello
STORED
set key2 0 900 5
world
STORED
# Verify data exists
get key1
VALUE key1 0 5
hello
END
# Flush the entire cache
flush_all
OK
# Attempt to get the data again
get key1
END
get key2
END

Modern client libraries provide a flush() method to execute this command. The following example shows how to use it with spymemcached in a Java application.

Ensure the spymemcached dependency is in your Maven pom.xml:

<dependency>
<groupId>net.spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.12.3</version>
</dependency>

This program sets two keys, verifies their existence, calls flush(), and then confirms that the keys have been invalidated.

import net.spy.memcached.MemcachedClient;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.internal.OperationFuture;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
public class MemcachedFlushExample {
public static void main(String[] args) {
String serverAddress = "127.0.0.1:11211";
try (MemcachedClient client = new MemcachedClient(AddrUtil.getAddresses(serverAddress))) {
System.out.println("Connection to Memcached server successful.");
// 1. Set a few keys
client.set("config:feature_flags", 0, "{\"new_dashboard\": true}").get();
client.set("user:45:session", 0, "active").get();
System.out.println("Set initial keys: 'config:feature_flags' and 'user:45:session'.");
// 2. Verify keys exist before flushing
System.out.println("Value of 'config:feature_flags' before flush: " + client.get("config:feature_flags"));
System.out.println("Value of 'user:45:session' before flush: " + client.get("user:45:session"));
// 3. Flush the cache
System.out.println("\n--- Flushing the cache --- ");
OperationFuture<Boolean> flushFuture = client.flush();
if (flushFuture.get()) {
System.out.println("Cache flush command sent successfully.");
} else {
System.err.println("Cache flush failed.");
}
// Note: The flush may not be instantaneous.
Thread.sleep(100); // Small delay to allow flush to propagate
// 4. Verify keys are gone after flushing
System.out.println("Value of 'config:feature_flags' after flush: " + client.get("config:feature_flags"));
System.out.println("Value of 'user:45:session' after flush: " + client.get("user:45:session"));
} catch (IOException | InterruptedException | ExecutionException e) {
System.err.println("An error occurred: " + e.getMessage());
Thread.currentThread().interrupt();
}
}
}

When you run this Java code, the output will be:

Connection to Memcached server successful.
Set initial keys: 'config:feature_flags' and 'user:45:session'.
Value of 'config:feature_flags' before flush: {"new_dashboard": true}
Value of 'user:45:session' before flush: active
--- Flushing the cache ---
Cache flush command sent successfully.
Value of 'config:feature_flags' after flush: null
Value of 'user:45:session' after flush: null