Script to Clear a Page from Varnish Cache

From Brian Nelson Ramblings
Revision as of 06:33, 19 January 2014 by Brian (Talk | contribs) (How to use the script)

Jump to: navigation, search

Script to Clear a Page from Varnish Cache

The script below will purge/ban a page from the varnish cache and then re-cache the page by curling it

#!/bin/bash

cmd="varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret"
site="http://www.briansnelson.com
page="$1"
 
if [ "$page" != "" ]; then
  echo -e "\nPurging old version from cache:"
  echo "-------------------------------"
  $cmd ban.url "^$page\$" | sed '/^$/d'
  $cmd ban.list | head -n 2
  echo -e "\nPopulating cache with new page content:"
  echo "---------------------------------------"
  curl -sL -w "%{http_code} %{url_effective}\n" $site/$page -o /dev/null
else
  echo Error: please provide a URL to purge..
fi

Once you have it download/setup on your system you will want to give the execute permission

chmod +x recache-varnish.script

How to use the script

bash recache-varnish.script <page to be purged/ban>
bash recache-varnish.script Main_Page

Or you can download the script here

You should just need to change the domain name

Additional Articles