Yii2 assets clear cache

Yii2 assets clear cache

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

Stefan Bogdanescu

Founder & Senior Architect

Yii2 Assets Clear Cache

In a standard Yii2 application, modifying files within infoweb\menu\module\assets will immediately affect the compiled output in backend\web\assets. This is not because of magic, but rather how the framework handles asset bundling. When you use the AssetBundle class, Yii2 compiles your JavaScript and CSS into a single file using tools like YII Assets or Grunt during development or deployment. Once this compilation occurs, the resulting files are cached in the backend\web\assets directory to serve them efficiently to users via HTTP caching headers.

If you edit a source file but do not clear this cache, the web server will continue serving the old, minified bundle because the filename (hash) remains the same until the next build process triggers a new one. Manually deleting files from backend\web\assets is fragile; it breaks versioning and makes rollbacks difficult if you accidentally delete the wrong file or miss an asset that hasn't been touched yet.

The robust solution involves leveraging Yii2's built-in development server or integrating a task runner like Gulp, Grunt, or Webpack to handle recompilation automatically. If you are running in development mode, simply restarting the PHP built-in server (php -S localhost:8080) often forces Yii2 to recompile assets on the fly. However, relying solely on restarts is inefficient for rapid iteration. A better approach is to configure your build process to watch the source directory and regenerate bundles whenever a file changes.

For example, if you are using Gulp with Yii2's asset compiler, you can set up a watcher that monitors infoweb\menu\module\assets. When a change is detected in that directory, Gulp triggers the compilation task, which outputs fresh files to backend\web\assets with a new hash appended to the filename. This breaks the browser cache automatically because the URL changes. Here is a conceptual snippet of how you might configure a watcher in your gulpfile.js:

const gulp = require('gulp');
const { exec } = require('child_process');

// Watch specific asset directory
gulp.watch(['infoweb/menu/module/assets/**/*.php', 'infoweb/menu/module/assets/**/*.js'], () => {
    // Trigger Yii2 asset compilation or your custom build task
    exec('php yii assets/compile'); 
});

In production environments, you cannot rely on the built-in server. Instead, you must ensure that your deployment script (e.g., composer install, npm run build, or a CI/CD pipeline step) explicitly clears old asset files and regenerates them after every code push. This guarantees that the backend\web\assets folder contains only the latest compiled bundles corresponding to the current source code.

There is a trade-off here between development convenience and production safety. Automatic clearing in production can lead to unintended side effects if your build script fails silently or if the hash generation logic changes unexpectedly. It is often safer to enforce a strict workflow where assets are built as part of the release process rather than relying on runtime caching invalidation.

If you prefer not to manage a complex build pipeline, consider using Yii2's AssetManager configuration to set a custom cache path that is easier to clear via command line tools like find or specific shell scripts. You can also configure Nginx or Apache to bypass asset caching for specific paths during development, though this sacrifices performance.

Ultimately, the goal is to decouple your source of truth—the code in infoweb\menu\module\assets—from the compiled output. By automating the recompilation step, you eliminate the need to manually browse the file system and delete old assets, reducing human error and streamlining the development workflow. For teams managing large-scale applications where asset integrity is critical, implementing a robust CI/CD pipeline that handles asset compilation as a mandatory step before deployment remains the industry best practice.

Our network