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:
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.
- PHP-FPM on Apache with split configuration per site
- Out with the iOS, in with the Android
Any ideas why ban takes around 10 seconds ?
You should update this post bacause it doesn’t work with varnish 5.2.
This post is on the first page on Google search.
Thanks anyway.
Hi, I should indeed update this post since it doesn’t document the newest Varnish versions anymore.
Dimitri, thanks for this post. Do you have any update for Varnish 4? Thanks in advance.
Hi, well actually I have already purged caches in Varnish 4 using the PURGE part in a vcl. So these things seem to still work in Varnish 4.
Hi ,
I tried using this command to purge ~$ curl -X PURGE http://www.yoursite.tld/ but i got an error like ” This method may not be used. “
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?
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.
I have tested this in Varnish 3 and haven’t been able to test this in Varnish 4 yet. I’ll update my post with the Varnish version.
To make it work with Varnish 4, instead of:
ban.url php-fpm
use
ban req.url ~ php-fpm