Flush Memcached Using Bash

less than 1 minute read

If you store application data in memcache, you may want to invalidate it once you deploy a new version to avoid corruption or weird results. There are several ways to do this but I recently tried one using nothing but Bash, and I like it.

Flush Memcache in Bash

Just add this to your deploy script:

$ echo "flush_all" | /bin/netcat -q 2 127.0.0.1 11211

(remember, all entries will be flushed. this is not the way to fly in high performance environments)

Bonus: Flush Disk Cache

Also, if you have cache files on disk, this is probably one of the best ways to trash them:

$ find YOUR/WEB/DIR/app/tmp/cache/ -type f -print0 | xargs -0 rm

It's actually a simplified version from what PHP uses to clean up session garbage files (see /etc/cron.d/php5)

What's good about this elaborate approach, is that it deals with

  • "argument list too long" by using find instead of a rm *
  • non-unix characters

print0 will delimit files by the 0 character, so you won't have to escape spaces or any other 'crazy' chars

Leave a Comment Right Here