Most of the time we need to update our
.env file, routes , views and app config files while our web application is under development or production version. Laravel application serves the cached data so caching problem occurs due to the robust cache mechanism of Laravel so our changes may not appear. To reflect these changes we need to clear the old cached data.
Caching data is very helpful for any website speed performance. It is common to cache the retrieved data for a time so it can be retrieved quickly on subsequent requests for the same data. The cached data is usually stored in a very fast data store such as Memcached or Redis in case of Laravel.
Laravel provides handful or artisan commands to cache as well as to clear the cached data. So let me show you how we can use these commands to reflect our changes/clear cached data and create new cache data.
Here the list of artisan commands to clear cache.
List of Laravel Cache
- Application Cache
- Configuration cache
- Route Cache
- View Cache
- Event cache
- optimize or All cache
Application Cache in Laravel
As I said previously Caching is a great way to speed up frequently accessed data in your application. Laravel stores these data as application cache. To clear these cache and test if your application is cache running properly or not we may run the cache clear command as follows:
$ php artisan cache:clear Application cache cleared!
This command uses the
cache:flush() facad method and clears all cached data from
Run the following command to clear application cache:
Configuration Cache in Laravel
To load all the application configurations quickly, Laravel stores all the app configuration options in single file and cached it as config cache. Basically its the
.env settings. That means when you have changed the
.env variables and you are not able to see the effect or change in that case you may clear configuration as follows:
Clear Configuration cache
$ php artisan config:clear Configuration cache cleared!
If you want to cache the configuration just after clearing it, Laravel provides one more option to you and that is the config clear command.
Clear and again cache configuration cache
$ php artisan config:cache Configuration cache cleared! Configuration cached successfully!
Route Cache in Laravel
Laravel caching system also takes routes in consideration, Caching your routes will drastically decrease the amount of time it takes to register all of your application's routes. When you add a new route, and it is not reflected. Then the following command will clear all route cache in your application:
$ php artisan route:clear Route cache cleared!
You can cache all the routes just after cleared as you have seen in config cache. To do that use the command as follows:
$ php artisan route:cache Route cache cleared! Routes cached successfully!
View Cache in Laravel
Laravel compiles all the views by default and caches for quick load it determines which view should be complied and when. So if your unable see the changes you made in some view files then you are supposed to run clear view command as below.
Clear view cache
$ php artisan view:clear Compiled views cleared!
Or you may run view cache to clear and re compile the view. This will clear all the view cache and recompiles a whole new set of view cache.
$ php artisan view:cache Compiled views cleared! Blade templates cached successfully!
Events Cache in Laravel
Laravel suggests to cache all your application events so, it will not look over all the listeners on each request. Caching events helps improve applications performance.
Clearing Events Cache
To clear your cached Events, run the following Artisan command:
$ php artisan event:clear Cached events cleared!
To cache all the events just after clearing it, you may run this command :
$ php artisan event:cache Cached events cleared! Events cached successfully!
Optimize Clear or Clear All Cache
As Laravel is very good at providing handful of useful artisan commands for rapid development. Here comes a combinations of all of the above explained commands, so you do not need to run each commands one by one.
To clear all Laravel's cache, run the following command:
$ php artisan optimize:clear Compiled views cleared! Application cache cleared! Route cache cleared! Configuration cache cleared! Compiled services and packages files removed! Caches cleared successfully!
To cache again just after cleared use
$ php artisan optimize Note that if you have used closure in any route it may throw
Unable to prepare route [/route] for serialization. Uses Closure
Compiled services and packages files removed! if for
$ php artisan clear-compiled which is self explaining
This command will not clear events as its obvious that every application may or may not have events
Some Additional commands
- Composer Cache
If you have changed some project file name, folder name or class of the file and your change is not being reflected then its time to clear composer cache
To auto load your application directory / classes
$ composer dump-autoload
you may want to clear composer cache, for that use this one :
$ composer clear-cache //Or $ composer clearcache //Or $ composer cc
Above three commands are same
- NPM Cache
If your change on any js libraries like vue js or css are not reflecting then you may consider run npm commands like:
// compile nmp files for development version $ npm run dev // Compile npm for the production version $ npm run prod // continuously monitor any changes on js files or css files(for laravel mix) $ npm run watch // Clears npm cache $ npm cache clean --force
Above I have listed all the Laravel's artisan commands about cache. And you can anytime use artisan list command to list all the option we can use.
// list all the commands and options $ php artisan list // Get help $ php artisan --help
Hope you get something useful in this article. If you have any question or doubt let me know in the comments. Thanks for reading this article.