setono / twig-cache-purger-bundle
A Symfony bundle that allows you to purge single templates in the Twig cache
Package info
github.com/Setono/TwigCachePurgerBundle
Type:symfony-bundle
pkg:composer/setono/twig-cache-purger-bundle
Requires
- php: >=8.1
- ext-filter: *
- symfony/config: ^6.0 || ^7.0 || ^8.0
- symfony/dependency-injection: ^6.0 || ^7.0 || ^8.0
- symfony/filesystem: ^6.0 || ^7.0 || ^8.0
- symfony/http-kernel: ^6.0 || ^7.0 || ^8.0
- symfony/twig-bundle: ^6.0 || ^7.0 || ^8.0
- twig/twig: ^2.0 || ^3.0
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: ^4.3 || ^5.1 || ^6.3
- setono/code-quality-pack: ^3.3
This package is auto-updated.
Last update: 2026-04-13 11:19:40 UTC
README
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.