Script to Clear a Page from Varnish Cache

From Brian Nelson Ramblings
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>

To clear this page from the varnish cache you would run:

bash recache-varnish.script Script_to_Clear_a_Page_from_Varnish_Cache

Or you can download the script here

You should just need to change the domain name

Additional Articles