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
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

flush_all
OK

quit

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 0.0.0.0/0 -p 11211 --open 2> /dev/null
...
Nmap scan report for something.tld (192.168.5.152)
PORT      STATE SERVICE
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 and podcaster. Currently working on DNS Spy. Follow me on Twitter as @mattiasgeniar.

I respect your privacy and you won't get spam. Ever.
Just a weekly newsletter about Linux and open source.

SysCast podcast

In the SysCast podcast I talk about Linux & open source projects, interview sysadmins or developers and discuss web-related technologies. A show by and for geeks!

cron.weekly newsletter

A weekly newsletter - delivered every Sunday - for Linux sysadmins and open source users. It helps keeps you informed about open source projects, Linux guides & tutorials and the latest news.

Share this post

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

Comments

Leave a Reply

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

Inbound links