How to clear complete cache in Varnish?

laurent picture laurent · Aug 11, 2016 · Viewed 50.3k times · Source

I'm looking for a way to clear the cache for all domains and all URLs in Varnish.

Currently, I would need to issue individual commands for each URLs, for example:

curl -X PURGE http://example.com/url1
curl -X PURGE http://example.com/url1
curl -X PURGE http://subdomain.example.com/
curl -X PURGE http://subdomain.example.com/url1
// etc.

While I'm looking for a way to do something like

curl -X PURGE http://example.com/*

And that would clear all URLs under example.com, but also all URLs in sub-domains of example.com, basically all the URLs managed by Varnish.

Any idea how to achieve this?

This is my current VCL file:

vcl 4.0;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    # Command to clear the cache
    # curl -X PURGE http://example.com
    if (req.method == "PURGE") {
        return (purge);
    }
}

Answer

laurent picture laurent · Sep 30, 2016

With Varnish 4.0 I ended up implementing it with the ban command:

sub vcl_recv {
    # ...

    # Command to clear complete cache for all URLs and all sub-domains
    # curl -X XCGFULLBAN http://example.com
    if (req.method == "XCGFULLBAN") {
        ban("req.http.host ~ .*");
        return (synth(200, "Full cache cleared"));
    }

    # ...
}