setono/twig-cache-purger-bundle

A Symfony bundle that allows you to purge single templates in the Twig cache

Maintainers

Package info

github.com/Setono/TwigCachePurgerBundle

Type:symfony-bundle

pkg:composer/setono/twig-cache-purger-bundle

Statistics

Installs: 25 537

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.1.0 2026-04-13 11:18 UTC

README

Latest Version Software License build codecov

Purge the cache for individual Twig template files instead of removing the whole cache directory.

Requirements

  • PHP 8.2+
  • Symfony 6.0/7.0/8.0
  • Twig 2.x or 3.x

Installation

composer require setono/twig-cache-purger-bundle

This will install the bundle and enable it if you're using Symfony Flex. If you're not using Flex, add the bundle manually to bundles.php:

Setono\TwigCachePurgerBundle\SetonoTwigCachePurgerBundle::class => ['all' => true],

Usage

The bundle provides a PurgerInterface service that you can inject into your services to purge individual Twig template cache files by their logical template name:

use Setono\TwigCachePurgerBundle\Purger\PurgerInterface;

final class YourService
{
    public function __construct(private readonly PurgerInterface $purger)
    {
    }

    public function updateTemplate(): void
    {
        // Purge the cached compiled version of a specific template
        $this->purger->purge('emails/welcome.html.twig');
    }
}

The purger resolves the logical template name to its compiled cache file, invalidates the opcache/APC bytecode cache, and deletes the file. The next time the template is rendered, Twig will recompile it from the source.