Laravel optimization commands

May 11, 2015 (updated April 5, 2021)

Laravel ships with a set of commands that optimize the framework for better performance. This article explains how the commands work, where the cache files are stored, and in what order they should run during deployment.

Side note: The intention of this article was to explain the somewhat confusing optimization commands that shipped with Laravel prior to version 5.1. With Laravel 5.1, released in June 2015, the commands and their logic have been cleaned up, and the article has been updated to reflect the changes.

php artisan optimize
php artisan config:cache
php artisan route:cache

Storage paths for optimizations

The optimization files generated by the artisan optimization commands are written to the bootstrap/cache/ directory. This directory should be writable by the web server (PHP) process.

artisan optimize (deprecated)

The optimize command is no longer needed due to recent improvements to the PHP op-code caching. The Laravel documentation advices you to remove all references to this command from your deployment scripts as it will be removed in a future Laravel release (though it's still present in Laravel 8). The following explanation of the command is kept as a reference.

php artisan optimize creates a compiled file of commonly used classes in order to reduce the amount of files that must be loaded on each request. The file is saved to, or overwrites, bootstrap/cache/compiled.php, which needs to be writable by the web server (PHP process).

You can specify additional classes to be included in the file by adding them to config/compile.php.

The compiled file is only created on production environments, unless the --force flag is used.

artisan optimize also creates bootstrap/cache/services.json which is used to optimize the loading of service providers. (The command does no longer compile views.)

php artisan clear-compiled reverses the process by deleting bootstrap/cache/compiled.php and bootstrap/cache/services.json.

NB! The ouput of artisan optimize may depend on your configuration files, e.g. the providers array from config/app.php. During deployment, run this command after php artisan config:cache.

artisan config:cache

php artisan config:cache combines all your configurations into one file for faster loading. The cache file is saved to bootstrap/cache/config.php. The command clears the old cache before creating a new one.

php artisan config:clear reverses the process by deleting bootstrap/cache/config.php.

artisan route:cache

php artisan route:cache creates a route cache file for faster route registration. The cache file is saved to bootstrap/cache/routes.php. The command clears the old cache before creating a new one.

php artisan route:clear deletes the cache file.

artisan view:clear

Laravel compiles view files the first time they're rendered by the framework, e.g. the first time a user visits your web app or site. The compiled file is named by calculating the SHA-1 hash of its path (not its content).

artisan view:clear clears all compiled view files from storage/framework/views/.

Older versions of Laravel

Prior to version 5.1, the optimization files generated by the artisan optimization commands were written to the vendor/ directory by default. If the vendor directory wasn't writable, the files were written to storage/framework/. As of v5.0.20 you were able to set storage/framework/ as the default storage path for optimizations by adding the following line to bootstrap/app.php:

$app->useStoragePathForOptimizations(true);

Starting with version 5.1, Laravel uses a dedicated directory for optimization files, bootstrap/cache, which contains compiled.php, routes.php, config.php, and services.json.