How to clear cache in laravel using artisan command

How to clear cache in laravel using artisan command

Hello friends, In this tutorial i will let you know different types of artisan commands to clear cache in laravel.  Sometimes we make changes to the application but that do not reflect instantly because of the laravel caching and we spend a lot of time to debug why changes didn’t reflect. So it may be a solution to clear the cache. 

Laravel can have multiple types of cache like route cache, view cache, application cache, config cache.

Laravel provides some artisan commands to clear these caches. We can clear these caches from the given commands by running it into terminal or can use it in controllers or directly in routes if we do not have terminal access.

So let’s see the caches and commands to clear these caches in laravel.

 

1) Clear Laravel Application Cache

By using the artisan command below, we can clear all the cache of laravel application.

php artisan cache:clear

And if we do not have terminal access then we can also use above cache clear command in route as written below.

Route::get('/clear-application-cache', function() {
    Artisan::call('cache:clear');
    return 'Application cache cleared';
});

2) Clear Route Cache

To clear the route cache we can execute the command below in the terminal or command prompt.

php artisan route:cache

We can run this command from a routes through a browser request.

Route::get('/clear-route-cache', function() {
    Artisan::call('route:cache');
    return 'Routes cache cleared';
});

3) Clear Config Cache

For clearing all the configuration caches we can use the following artisan command.

php artisan config:cache

And we can call the above command in a route as given below.

Route::get('/clear-config-cache', function() {
    Artisan::call('config:clear');
    return 'Config cache cleared';
});

4) Clear View Cache

For clearing all the view caches we can use the following artisan command.

php artisan view:cache

And we can call the above command in a route as given below.

Route::get('/clear-view-cache', function() {
    Artisan::call('view:clear');
    return 'View cache cleared';
});

Reoptimized Class

After clear the caches, we can reoptimize the classes from the command given below.

php artisan optimize

You can also clear the caches by deleting all the files in cache directory under bootstrap and storage directory.