May 11, 2023

How to properly clean the Varnish Cache in a WordPress blog.

A snippet of PHP code to integrate into your theme to clean your Varnish Cache in a fast, versatile and selective way.

Varnish Cache is an extremely powerful and flexible software technology that is widely used as an HTTP accelerator. This tool acts as an intermediary between the server and the client, storing data (or "caching") to reduce the load on the server and provide faster responses to clients.

Varnish Cache Reverse Proxy Mechanism

The benefits of Varnish Cache.

The benefits of Varnish Cache are many. First of all, it significantly increases website performance by reducing page loading time and saving server resources. Secondly, it improves the scalability of websites by allowing them to handle more requests at the same time. Finally, it offers unparalleled flexibility thanks to its VCL (Varnish Configuration Language) configuration language, which allows you to define specific caching rules for each case.

Varnish and the TTFB

Varnish Cache has a significant impact on Time to First Byte (TTFB), a key indicator of website performance. TTFB is the time between when a client (such as a web browser) sends an HTTP request and when it receives the first byte of data from the server. Varnish Cache drastically reduces TTFB by storing server responses and quickly serving them to clients, avoiding the need to reprocess requests.

A lower TTFB means content is delivered to users faster, improving the user experience and perception of site speed. But it's not just the user experience that benefits: search engines like Google also appreciate the faster response times.

Varnish and Google's Budget crawl

Google uses a limited resource, known as a "crawl budget," to index websites. This budget is the number of pages that Google is able and willing to crawl in a given period of time. A lower TTFB means Google can crawl and index more pages within this budget, optimizing the use of its crawling resources.

Improving your crawl budget can have a direct impact on Google's ability to index your website in a timely manner. This, in turn, can positively influence your site's ranking in search results, as more comprehensive and timely indexing gives Google a more accurate view of your site and its content. In this way, the use of Varnish Cache can help improve both the speed of the site and its ranking in search results.

Stats Google Crawl Response Time

High profile websites like The New York Times, BBC, Wikipedia have already adopted Varnish Cache to improve their performance and ensure a smooth and responsive user experience.

Varnish and WordPress

WordPress is the most popular blogging platform in the world and Varnish Cache fits its needs perfectly. However, managing Varnish Cache in a WordPress environment can be tricky. Fortunately, there are several plugins available that can help with this process, including W3 Total Cache e Varnish HTTP Purge.

Despite their usefulness, these plugins can present serious problems.

W3 Total Cache, for example, can be extremely slow at clearing the cache, especially when dealing with multiple categories. This can lead to significant delays in publishing posts, interfering with editorial activity and potentially causing errors such as Gateway Timeout.

NGINX Gateway Timeout

Varnish HTTP Purge, while being more streamlined and focused on cleaning the cache only, has proven to be unreliable on several occasions. Instead of selectively and granularly cleaning the cache, it often cleans the entire cache, which can have a significant impact on site performance.

To solve these problems, we propose a snippet of PHP code to insert in the functions.php file of your active WordPress theme. This code takes care of clearing the Varnish cache every time a post is published (except for drafts), ensuring that the home page, the article itself and all related categories are correctly updated.

The proposed code does not purport to deal with WooCommerce-installed WordPress installations, product taxonomies, AMP tags, or APIs; however, it is quite simple to understand and easily extendable to adapt to the most disparate needs.

PHP Code for WordPress to PURGE Varnish.

// Funzione che pulisce la Cache Varnish tramite metodo PURGE su socket RAW senza l'utilizzo di cURL

function purgeURL($URL) {

$varnishIP = "";
$varnishPORT = "80";

$hostwp = trim(str_replace( array( 'http://', 'https://' ), '', get_option('siteurl')), '/' );

// Open the socket
$errno = ( integer) "";
$errstr = ( string) "";
$varnish_sock = fsockopen( "", "$varnishPORT", $errno, $errstr, 10);

// Prepare command to send
$cmd = "PURGE $URL HTTP/1.0\r\n";
$cmd .= "Host: $hostwp\r\n";
$cmd .= "Connection: Close\r\n";
$cmd .= "\r\n";

// Send the request
fwrite( $varnish_sock, $cmd);

// Close the socket
fclose( $varnish_sock);

// In fase di salvataggio POST recupero gli slug delle categorie che contengono il post, ed invoco la pulizia della cache Varnish
// tramite la funziona purgeURL

function pulisci_cache_categorie ($post_id){

if ( wp_is_post_revision( $post_id ) ) {

$categories = get_the_category($post_id);

// Inizializzare un array vuoto per i permalink
$relative_permalinks = array();

// Iterare su ogni categoria
foreach ($categories as $category) {
// Ottenere il permalink della categoria e convertirlo in un URL relativo
$relative_permalink = wp_make_link_relative(get_category_link($category->term_id));

// Aggiungere l'URL relativo all'array
$relative_permalinks[] = $relative_permalink;

foreach($relative_permalinks as $relative_permalink) {
purgeURL ($relative_permalink);


add_action( 'save_post', 'pulisci_cache_categorie' );

The provided code is written in PHP and is part of the file functions.php of a WordPress theme. This code defines two functions: purgeURL() e pulisci_cache_categorie().

  • purgeURL($URL): This feature is designed to interface with a Varnish server, a very powerful web application accelerator, to invalidate or "purge" a particular URL from the cache. The Varnish server is configured to reside locally (“”) and communicate on port 80. The function creates a socket connection with the Varnish server and sends a “PURGE” command for the specified URL. This command tells Varnish to remove any cache corresponding to that URL, forcing Varnish to fetch a fresh copy of the page the next time it's requested. After sending the command, the function closes the socket connection.
  • pulisci_cache_categorie($post_id): This function is linked to the 'save_post' WordPress action, which fires every time a post is saved. When a post is saved, this function is called with the post ID as an argument. If the post is a review (that is, it's not the "live" version of the post), the feature terminates immediately. Otherwise, it fetches all the categories the post belongs to and builds an array of relative URLs corresponding to these categories. For each of these URLs, call the function purgeURL(), causing these pages to be removed from the Varnish cache. This ensures that when a post is edited, all category pages that include that post are updated in the cache, thus reflecting the edits to the post.

These features work together to make sure that post changes in WordPress are reflected in your category pages in a timely manner, while retaining the performance benefits of a web cache like Varnish.

A focus on performance thanks to fsockopen().

In the context of network calls in PHP, fsockopen() and cURL are two popular methods of making HTTP requests. While cURL is known for its versatility and wide range of configuration options, fsockopen() may offer speed advantages in certain scenarios.

fsockopen() opens a direct socket to a server, allowing for faster and less resource intensive low-level communication. This can be especially beneficial when you are making many simultaneous requests or have very low latency needs. Furthermore, fsockopen() may be faster in situations where the server's response is not needed or can be ignored, since there is no need to wait for the response to be returned and processed as it would be with cURL.

Do you have doubts? Not sure where to start? Contact us

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.


Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

INFORMATION is the leading Italian provider of high performance hosting solutions. Our subscription model is affordable and predictable, so customers can access our reliable hosting technologies, dedicated servers and the cloud. also offers excellent support and consulting services on Hosting of the main Open Source CMS such as WordPress, WooCommerce, Drupal, Prestashop, Magento.


Would you like to see how your WooCommerce runs on our systems without having to migrate anything? 

Enter the address of your WooCommerce site and you will get a navigable demonstration, without having to do absolutely anything and completely free.

No thanks, my customers prefer the slow site.
Scroll to Top