How to Clear cache in Laravel 10/9/8/7

In this simple tutorial I will show you how to clear cache in Laravel application easily. I will also show you the artisan commands to run and I will also show you how you can create a simple function to clear cache via web url.

Why clear cache in Laravel?

Sometimes you make certain changes to your Laravel config or views but changes do not reflect in your app console or in the frontend. This may primarily happen because many elements, specially some of your app’s configuration, routes and media such as images, Javascript, CSS  are served by Laravel application cache. So it becomes essential to clear cache in Laravel application to see latest changes in effect.

Clear Cache in Laravel using CLI (Terminal)

Open a terminal in your Laravel application root directory. You can run the following commands to clear various types of cache in Laravel:

1. Clear Application Cache

Run the following command to clear application cache in Laravel application.

php artisan cache:clear

2. Clear Route Cache

To clear route cache of your Laravel application execute the following command.

php artisan route:clear

3. Clear Config Cache

You can use config:clear to clear the config cache of the Laravel application.

php artisan config:clear

4. Clear Compiled View Cache

Also, you may need to clear compiled view files of your Laravel application. To clear compiled view files run the following command from the terminal.

php artisan view:clear

In addition, Laravel  provides Artisan command to precompile all of the views used by your application. So can clear and rebuild all view cache:

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

5. Events Cache

If you are using Events in your Laravel application, it is recommended to cache your Events, so that Laravel framework does not need to scan all of your event listeners on every request.

$ php artisan event:clear
Cached events cleared!

6. Clear ALL Cache

The last but not the least one. You can clear ALL; config, view, application, routes, events cache & compiled services and packages files, in a single go:

php artisan optimize:clear

The last one resulted in:

Cached events cleared!
Compiled views cleared!
Application cache cleared!
Route cache cleared!
Configuration cache cleared!
Compiled services and packages files removed!
Caches cleared successfully!

Clear Cache in Laravel (via URL)

This is useful where you don’t have access to cli. For example, shared hosting may not provide ssh access to server. In this case, you can write s small anonymous function, bind it to a route and call it in your browser.

Create a new route in your routes/web.php route file of your Laravel Application with code:

Route::get('/clear-cache', function() {
    Artisan::call('cache:clear');
    Artisan::call('route:clear');
    Artisan::call('config:clear');
    Artisan::call('view:clear');
    Artisan::call('event:clear');
    return "All Cache was cleared";
});

Or just:

Route::get('/clear-cache', function() {
    Artisan::call('optimize:clear');
    echo Artisan::output();
});

The result of last one should be the same as the CLI command php artisan optimize:clear above.

Conclusion

This tutorial demonstrates with help of examples to clear cache in Laravel framework using command line interface (CLI) and PHP script. Clearing cache in Laravel is safe because when you clear the cache the application starts creating new cache again.

2 thoughts on “How to Clear cache in Laravel 10/9/8/7

Leave a Reply