All Collections
Server setup and configuration
Templ Cache - Advanced use cases
Templ Cache - Advanced use cases

How to purge/flush Templ Cache programmatically

Emanuel avatar
Written by Emanuel
Updated over a week ago

The Templ Cache have an option for automatic purging of cache, which is all that is needed in most cases.

If that's not enough for you however, here's some examples of more advanced ways of purging Templ Cache.

How to purge Templ Cache using PHP

In case you need to purge the cache using PHP code, here's how one can do that:

$templio_cache = new templioCache();
$templio_cache->purge_zone_once();

Automatic purging using action hooks

When automatic purging of the cache has been enabled in the Templ Cache settings, the cache is automatically purged when certain actions are being run.

If you want the cache to be purged by additional actions, here's an example on how to add the action "my_action":

add_filter('templio_cache_purge_actions', function($purge_actions) {
$purge_actions[] = 'my_action';
return $purge_actions;
});

Purge cache over SSH

Sometimes it can be useful to purge the cache over SSH. Here's how to do that:

ssh -p <PORT> <USER>@<IPADDR> /usr/bin/templ-ccli cache purge

Purge cache over HTTP

In other cases, it can be useful to create a "webhook" to let the cache be flushed by a HTTP request.

To do that, create a plugin file named templ_cache_purger.php` with the following content:

<?php
/**
* Plugin Name: Templ Cache Purger
* Description: A tiny plugin that allows for purging of cache when a certain URL is called.
* Version: 1.0
*/

function templ_cache_purge() {
if(!class_exists('templioCache')) {
return;
}
$templio_cache = new templioCache();
$templio_cache->purge_zone_once();
}

add_action('plugins_loaded', function() {
if(!isset($_GET['templ_cache_purge'])) {
return;
}
templ_cache_purge();
die;
});

The cache can then be purged by sending a request to https://example.com/?templ_cache_purge

Did this answer your question?