How To Flush The Entire Contents Of Memcache Server

by lifeLinux on January 28, 2012

If you use Memcached server to store application data, you may want to invalidate it once you deploy a new version to avoid corruption or weird results… In this article, I’ll show you How do I flush the entire contents of a Memcached server ?

Using telnet command

I often use telnet to flush the entire contents of Memcached server, type the following command

# telnet localhost 11211

Output

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
flush_all
OK
quit
Connection to localhost closed by foreign host.

Where
– localhost: Memcached server instance
– 11211: Memcached server port

Using netcat command

# echo "flush_all" | nc localhost 11211

By default, nc (or netcat) creates a TCP socket either in listening mode (server socket) or a socket that is used in order to connect to a server (client mode). Actually, netcat does not care whether the socket is meant to be a server or a client. All it does is to take the data from stdin and transfer it to the other end across the network.

Restart Memcached Server

Login as root and type the following command

# /etc/init.d/memcached restart

Restarting your application is not ideal however, you will lose anything cached in memory, cause delays to users trying to access your site, that sort of thing.

Using PHP script

Create php file with the following content

# vi flush_memcached.php
$memcache = new Memcache;
$memcache->connect('localhost', 11211);
$memcache->flush();

To execute this script, type the following command

# php flush_memcached.php

Related Posts:

Previous post:

Next post: