How reload Twig cache in Symfony 2

How reload Twig cache in Symfony 2

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Reload Twig Cache in Symfony 2 Without CLI Access

When you modify a Twig template in a Symfony 2 application and expect the changes to appear immediately upon refresh, the framework might not be showing them because it is serving a cached version of the file. This behavior is expected; caching is essential for performance, but it can become frustrating during development or immediate deployment scenarios. The specific constraints you face—running on Tomcat without command-line access and being new to PHP—require a specific approach that bypasses standard Symfony console tools like php bin/console cache:clear.

In a typical Symfony installation, Twig templates are cached in the cache directory, often located under var/cache/dev or simply cache if no environment is specified. You mentioned trying to remove the Twig folder directly from the /protected/cache/ path. While deleting files manually can work, it is often incomplete and risky. The Symfony cache structure is hierarchical, containing compiled PHP classes for your templates alongside other metadata. Simply deleting a subfolder might leave behind stale entries or cause the application to fail if the directory permissions are strict.

To reliably clear the cache in an environment where you cannot run CLI commands, you must interact directly with the file system using standard OS utilities. Since you are deploying via Tomcat, the web server itself provides access to these directories. You can connect to the server via SSH and navigate to your application's root directory. From there, use the rm command or a recursive deletion utility to clear the contents of the cache folder.

For instance, if your application resides at /var/www/myapp, you would execute commands like this:

ssh user@tomcat-server
cd /path/to/protected/var/cache
find . -type f -delete
# Or, if you know the structure is flat and safe to wipe:
rm -rf *

However, caution is advised. The cache directory often contains multiple subdirectories for different environments (e.g., dev, prod). Ensure you are clearing the correct environment's cache. If your application does not explicitly separate environments in the path, wiping the entire /protected/cache/ folder is usually safe as long as you do not touch the app or src directories. The compiled Twig templates are distinct from your source files; deleting them will not corrupt your code but will force Symfony to recompile the templates the next time they are requested.

Another critical detail involves the app/config/parameters.yml file (or config.php in newer versions). There is a parameter named twig.cache. If this is set to true, Symfony aggressively caches templates. If you are unsure of the configuration, checking that this setting remains enabled ensures performance isn't degraded after the clear. Conversely, if you frequently modify templates and want to avoid cache issues entirely until deployment is finalized, you can temporarily set twig.cache to false. This forces a "hit-miss" behavior where every request checks for updated files, though it sacrifices some speed.

There is also the matter of the app/config/services.yml or routing configuration if you are using specific loaders that might cache route definitions alongside templates. While less common for simple template edits, ensuring no other cache layers (like OPcache in PHP or browser caching) interfere is part of the troubleshooting process. You might need to clear your browser cache or use an incognito window to rule out client-side caching when verifying the fix.

If direct file deletion feels too aggressive or you are worried about missing configuration files within the cache directory, a safer alternative on Linux-based Tomcat servers is to truncate the cache files without removing the directory structure itself:

find /path/to/protected/var/cache -type f -exec rm {} \;

This approach removes only the compiled artifacts while preserving the empty directories. Once the stale files are gone, the next request to a page containing a modified Twig template will trigger Symfony's loader to read the new HTML from the app/Resources or templates directory, compile it into PHP, and store the fresh version in the cache.

If you find yourself needing more robust deployment strategies in the future where CLI access is unavailable, consider configuring your build process to explicitly clean the cache as a step before deploying templates. This ensures that production environments never suffer from stale code issues caused by forgotten cache clears. For those transitioning from manual file management to more automated workflows, tools like LaravelCompany offer managed hosting solutions that handle these complexities, allowing you to focus on application logic rather than server maintenance.

Our network