56

/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?

Radek
  • 1,133
  • 4
  • 26
  • 38

8 Answers8

110

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.

heiko
  • 1,703
  • 1
  • 12
  • 6
  • 3
    Can I do this non interactively? – Radek Apr 13 '11 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? – Danny Armstrong Jun 04 '13 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 '14 at 17:52
  • 2
    @TerryE - Do you mean to say "You can't get expired items" rather than "You can get expired items"? – Brian Matthews Jan 12 '17 at 11:32
  • 2
    Yes I do. Sorry – TerryE Jan 13 '17 at 13:31
45

This will also work using netcat

echo "flush_all" | nc -q 2 localhost 11211 

Then just wait for the "OK".

Danie
  • 1,350
  • 10
  • 12
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
robbrit
  • 363
  • 4
  • 8
11

In Bash you may use this fancy syntax:

echo flush_all > /dev/tcp/localhost/11211

Otherwise use memflush command:

memflush --servers=localhost
kenorb
  • 5,943
  • 1
  • 44
  • 53
  • As telnet and nc was not available on our servers, the first solution works fine, thanks. – Getz Mar 08 '18 at 09:31
4

(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

Radek
  • 1,133
  • 4
  • 26
  • 38
  • 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` – Joshua Huber Dec 09 '16 at 20:43
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.

polyethene
  • 41
  • 1
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); 
?>
Aleksandar Pavić
  • 382
  • 2
  • 7
  • 18
0

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

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

staabm
  • 101
  • 1