55

/opt/eduserver/eduserver gives me options:

Usage: /opt/eduserver/eduserver {start|stop|startphp|startwww|startooo|stopphp|stopwww|stopooo|restartphp|restartwww|restartooo|status|restart|reload|force-reload}

where memcache is php module there is memcache.ini in /opt/eduserver/etc/php/conf.d.

I want to clear the memcache from command line. Can I do it somehow without 'touching' any other part of the web server?

8 Answers 8

108

yes. you can clear the memcache. try:

telnet localhost 11211
flush_all
quit

if the memcache does not run on localhost 11211, you will have to adjust it.

5
  • 3
    Can I do this non interactively?
    – Radek
    Apr 13, 2011 at 23:54
  • 2
    I'm trying to verify this actually was effective. So far stats still shows total_items 3926. I was expecting total_items to drop to 0. Is that not how to verify the cache was flushed? Jun 4, 2013 at 18:01
  • 5
    @DannyArmstrong, flush only marks items as expired. Memcache doesn't remove expired items from the cache straight away for performance reasons. You can get expired items, so they have vanished from an API perspective
    – TerryE
    Jul 29, 2014 at 17:52
  • 2
    @TerryE - Do you mean to say "You can't get expired items" rather than "You can get expired items"? Jan 12, 2017 at 11:32
  • 2
    Yes I do. Sorry
    – TerryE
    Jan 13, 2017 at 13:31
45

This will also work using netcat

echo "flush_all" | nc -q 2 localhost 11211 

Then just wait for the "OK".

3
  • 4
    w/o hardcoded 2 seconds: printf "flush_all\nquit\n" | nc -q -1 127.1 11211
    – youfu
    Apr 30, 2015 at 15:50
  • 3
    OSX needs -w for timeout: echo "flush_all" | nc -w 2 localhost 11211
    – mahemoff
    Oct 14, 2015 at 6:51
  • w/o quit & wait: printf "flush_all" | nc localhost 11211 Feb 20, 2017 at 11:26
26

memcflush in the memcache tools is what you want:

memcflush --servers=localhost:11211

Change localhost to whatever your server is.

The memcache tools may not be installed on the server, if you're running a Debian-based OS you can install it like this:

sudo apt-get install libmemcached-tools
1
  • 2
    In Red Hat distributions, the utility is included in libmemcached.
    – Kevin
    May 23, 2016 at 18:44
10

In Bash you may use this fancy syntax:

echo flush_all > /dev/tcp/localhost/11211

Otherwise use memflush command:

memflush --servers=localhost
1
  • As telnet and nc was not available on our servers, the first solution works fine, thanks.
    – Getz
    Mar 8, 2018 at 9:31
4

Rather than waiting for timeouts you can make the command instantaneous by following flush_all with the quit command:

printf "flush_all\r\nquit\r\n" | nc localhost 11211

Alternatively if you don't have nc:

printf "flush_all\r\nquit\r\n" > /dev/tcp/127.0.0.1/11211

Though this method won't produce an output, although you can verify it works by checking stats to see that cmd_flush increased.

3

(sleep 2; echo flush_all; sleep 2; echo quit; ) | telnet 127.0.0.1 11211

if you want to run it non-interactively

thanks to @heiko

1
  • This worked for me, as I had a host where I didn't want to install nc. Note that I worked fine for me without either the initial sleep` or the final quit. I did ( echo 'flush_all' ; sleep 2 ) | telnet myhost.fqdn.com 11211 Dec 9, 2016 at 20:43
0

in case you use a socket for connecting to memcached the syntax is

echo "flush_all" | nc -U ~/memcached.sock

0

Here is a function to flush memcached via PHP, in case that you need to refresh it without logging into ssh...

You can just http://yourserver.com/memflush.php

Call this file memflush.php

<?php

 $socket = fsockopen("localhost", "11211", $errno, $errstr); 

    if($socket) { 
        echo "Connected. <br /><br />"; 
    }
    else { 
        echo "Connection failed!<br /><br />"; 
    }

    fputs($socket, "flush_all\r\n"); 
    $buffer = ""; 

    fclose($socket); 
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.