How to purge the Varnish cache

Edit: This guide was created using Varnish 3, it is possible that some action will not work with Varnish 4.

In one of my previous posts I have guided you into configuring Varnish for WordPress websites. One of the main purposes of Varnish is caching and it is great at it.
Yet you may come into a situation where you would want to view the actual site, page or file instead of the cached version of it. Developers for example would immediately like to see their changes instead of waiting for ages for the TTL of an object to end.

This is why Varnish allows you to purge (aka “ban”) one or more objects from the cache. There are different ways to purge the Varnish cache so I will explain them one by one.

Restart Varnish

The easiest and quickest way to purge your entire cache is simply to just restart Varnish on your server. This can be achieved by executing the following command:

~$ /etc/init.d/varnish restart

As I mentioned before, this will purge the entire content of Varnish instead of a single (or more) object(s).

Purge using VCL

Varnish allows you to add a function into your VCL which can be used to purge specific objects from its cache. In order to use this function you will need the following content into your Varnish configuration file (in my case this is just the file /etc/varnish/default.vcl).

acl purgers {                                                           # Who is allowed to purge?
    "localhost";
    "127.0.0.1";
    "wheezy.dimitri.eu";                                                # Just my server its hostname
}
 
sub vcl_recv {
    if (req.request == "PURGE") {
        if (!client.ip ~ purgers) {
            error 405 "You are not allowed to purge";
        }
            return(lookup);
    }
}
 
sub vcl_hit {
    if (req.request == "PURGE"){
        set obj.ttl = 0s;
        error 200 "Varnish cache has been purged for this object.";
    }
}
 
sub vcl_miss {
    if (req.request == "PURGE") {
        error 404 "Object not in cache.";
    }
}

Now you can purge a cached object yourself. I use curl to call the PURGE function like this:

~$ curl -X PURGE http://www.dimitri.eu/php-fpm-apache-split-config-per-site/

Resulting in the following response:

Purge Varnish cache

The next visit to this page will result in a miss from the cache whereafter this page will be cached again.
You can still flush the entire cache for a domain by using the following command:

~$ curl -X PURGE http://www.yoursite.tld/

Using the command line

You can also use the Varnish command line tool called varnishadm. Just invoke the following command to enter the Varnish command line:

~$ varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret
varnish> ban.url php-fpm
200

I have now purged every url matching string “php-fpm”. You can throw any regular expression towards the ban function to purge the cache for multiple objects at once.

~$ varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret
varnish> ban req.http.host ~ www.dimitri.eu && req.url ~ .css
200

I have now purged every .css object for my site resulting in a cache miss the first time I request a css file.

Tagged on: , ,

7 thoughts on “How to purge the Varnish cache

    1. Dimitri Steyaert Post author

      Hi,
      Can you verify the next things?

      1. Are you sure you put the
      if (req.request == "PURGE") {
      ...
      }

      in your sub vcl_recv block?

      2. Are you running Varnish 3 or 4?

      3. Can you test your Varnish config with the following command:
      varnishd -C -f /etc/varnish/default.vcl
      Any error should be fixed to make this work

      4. Can you verify that you are trying to send the PURGE to the Varnish service and not your webservice (Apache, Nginx,…) by checking the ports (80, 8080) they run on?

  1. idem2lyon

    Hi,

    seems taht it doesn’t work in varnish 4

    # varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret
    200
    —————————–
    Varnish Cache CLI 1.0
    —————————–
    Linux,3.13.0-37-generic,x86_64,-sfile,-smalloc,-hcritbit
    varnish-4.0.2 revision bfe7cd1

    Type ‘help’ for command list.
    Type ‘quit’ to close CLI session.

    varnish> ban.url php-fpm
    101
    Unknown request.
    Type ‘help’ for more info.

Leave a Reply

Your email address will not be published. Required fields are marked *