Skip to content

Is it possible to clear a specific template cache using a specific key since the template cache refactor in Craft CMS 3.5+? #15745

Closed Answered by brandonkelly
jamesmacwhite asked this question in Q&A
Discussion options

You must be logged in to vote

You can register a custom cache invalidation tag to the cache:

{% cache %}
  {% do craft.app.elements.collectCacheTags(['custom-tag']) %}
  ...
{% endcache %}

Then clear caches by that tag in your queue job:

use yii\caching\TagDependency;

TagDependency::invalidate(Craft::$app->cache, 'custom-tag');

For extra credit, you can register the tag with the Caches utility, which will add a checkbox for the tag under “Invalidate Data Caches”:

use craft\base\Event;
use craft\events\RegisterCacheOptionsEvent;
use craft\utilities\ClearCaches;

Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_TAG_OPTIONS, function(RegisterCacheOptionsEvent $event) {
    $event->options[] = [
        'tag' => '

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jamesmacwhite
Comment options

Answer selected by brandonkelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants