All About Laravel: PHP Artisan Cache Commands

Sometimes in our Laravel application, we find that the changes in code are not reflecting on browse. This is because of the caching by Laravel. To bring those changes back we need to clear the cache using some commands. In this blog, we will discuss cache-related issues and how to hand them in your Laravel application.

Clearing All Cache

Laravel provides a handy Artisan command that helps clear the caches of a Laravel application. It is a convenient way to reset all cache in a Laravel application. To clear all Laravel’s cache, just 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!

Note: All cache types that existed in your Laravel application will be cleared entirely, except the Events cache.

We can also clear specific cache types by following the commands below:

Configuration Cache:

This cache loads all of the configuration options for the Laravel application into a single file which will be loaded quickly by the framework.

Clearing this configuration cache has 2 options. One is to just clear the cache and another is setting the configuration.

$ php artisan config:clear
Configuration cache cleared!

and for setting:

$ php artisan config:cache 
Configuration cache cleared! 
Configuration cached successfully!

as you see,

php artisan config:cache

does both clearings and setting the cache. It’s better to run this command instead.


Views Caching

Views are cached into compiled views to increase performance when a request is made. By default, Laravel will determine if the uncompiled view has been modified more recently than the compiled view, before deciding if it should recompile the view.

However, if for some reason the views are not reflecting recent changes, running the following command will clear all compiled views cache:

$ php artisan view:clear
Compiled views cleared!

In addition, Laravel also provides an Artisan command to precompile all of the views utilized by a Laravel application. Similarly, the command also clears the view cache before recompiling a new set of views:

$ php artisan view:cache
Compiled views cleared!
Blade templates cached successfully!

Route Caching

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, you will have to clear your route cache for the new route to take effect.

The following command will clear all route cache :

$ php artisan route:clear 
Route cache cleared!

To cache the routes again, just need to run the following command:

$ php artisan route:cache 
Route cache cleared! 
Routes cached successfully!

Running the above command alone is enough to clear your previous route cache and rebuild a new one. It’s also better to run this command instead.


Events Cache

If Events are used in a Laravel application, it is recommended to cache all Events, as it likely does not want the framework to scan all of the listeners on every request.

When you want to clear your cached Events, you may run the following Artisan command:

$ php artisan event:clear
Cached events cleared!

Likewise, caching your Events also clear any existing cache in the framework before a new cache is rebuilt:

$ php artisan event:cache
Cached events cleared!
Events cached successfully!

Application Cache

Using Laravel’s Cache is a great way to speed up frequently accessed data in your application. While developing your application involving cache, it is important to know how to flush all cache correctly to test if your cache is working properly.

To clear your application cache, you may run the following Artisan command:

$ php artisan cache:clear 
Application cache cleared!

This will clear all the cache data in storage which are typically stored in /storage/framework/cache/data/. which is also similar to calling the Cache::flush(); Facade method via code.

Note: This command will NOT clear any configroute, or view cache, which are stored in /bootstrap/cache/ directory.