Joel Vardy

Delete Specific Memcached Keys

Posted: 24 April 2015

Using in memory storage to cache data is very common, however the ability to delete keys is not easily possible from the command line. I would always end up restarting the memcached service, but this would of course delete all keys, which is not always desirable.

I built a simple command line application which can be installed globally on a system using Composer, this post will outline how to use the tool.

Installation

To install the tool globally run:

composer global require joelvardy/memcachedtool

You will also need to ensure the Composer binary directory is included in your path, for example:

echo "PATH=\$PATH:\$HOME/.composer/vendor/bin" >> ~/.bashrc
source ~/.bashrc

Commands

Stats

You can view several stats regarding the memcached server by running:

memcachedtool stats

View Keys

It is possible to show keys which are stored on the memcached server, for example:

# Show all keys
memcachedtool keys

# Show keys matching a regular expression
memcachetool keys --regex "/^jv_post_(.*)$/"

Delete Keys

Deleting keys was why I built this tool, you can delete keys in three different ways:

# Delete all keys
memcachedtool delete --all

# Delete specific key
memcachedtool delete --key jv_posts

# Delete keys matching a regular expression
memcachetool delete --regex "/^jv_post_(.*)$/"

Source Code

For more information, and to see how I built this tool please have a look at the code available on GitHub.