Flush all the content from Memcached via the CLI

Mattias Geniar, Tuesday, January 6, 2015

Memcached is an easy to use key/value store run in memory of a server. It's content is volatile, every restart of the Memcached service would remove all the data in memory and start anew.

But you can also flush the content (all keys and their values) from the command line, without restarting Memcached or additional sudo commands to grant non-privileged users permissions to flush the cache.

To flush the content, you can telnet to your Memcached instance and run the flush_all command.

$ telnet localhost 11211
Connected to localhost.
Escape character is '^]'.



Or in several one-liners that you can use in scripts, they rely on nc (netcat; yum install nc):

$ echo "flush_all" | nc localhost 11211
$ nc localhost 11211 <<< "flush_all"

As soon as the flush_all command is typed, all keys are set to expire. They won't be dropped from memory actively (as this would be quite a "heavy" operation to drop every key), but they're expired so the next retrieval would be invalid. This also means the flush_all doesn't free memory on the server, it frees the memory in the memcached service.

As a bonus, here's an nmap command that scans the entire IPv4 IP space for services that listen to port 11211. If you want to have fun, write a script that loops each IP and sends flush_all's to each service, every minute.

$ nmap -p 11211 --open 2> /dev/null
Nmap scan report for something.tld (
11211/tcp open  unknown

That's the price you pay for not correctly limiting your Memcached services. ;-)

Hi! My name is Mattias Geniar. I'm a Support Manager at Nucleus Hosting in Belgium, a general web geek & public speaker. Currently working on DNS Spy & Oh Dear!. Follow me on Twitter as @mattiasgeniar.

Share this post

Did you like this post? Will you help me share it on social media? Thanks!


Leave a Reply

Your email address will not be published. Required fields are marked *

Inbound links