Laravel Clear Cache Using Artisan Command and Programmatically

Laravel Clear Cache Using Artisan Command and Programmatically

In this laravel tutorial, you will learn how to clear cache from blade (views), routes, and config using the artisan command with the command line and programmatically without the command line in laravel apps.

When your app is in production mode and you need to implement caching in Laravel app or project to increase performance, it is important to note if you migrate the Laravel web application from development mode to production mode. so before that you have to clear the cache of your laravel web application

There are two methods to clear all types of caches such as root cache, view cache, app cache in laravel; the methods are the following:

Method 1: Clear Cache in Laravel using Artisan Command line

If you want to clear cache using commnad then open your terminal and go to the laravel application’s folder and execute below commands:

Clear Route Cache

To clear the route cache in Laravel using the PHP Artisan command-line interface, you can use the route:cache command.

php artisan route:cache

Clear Laravel Application Cache

You can clear laravel application cache by using the php artisan cache:clear command in Laravel:

php artisan cache:clear

Laravel Clear Config Cache

To clear the config cache in Laravel using the PHP Artisan command-line, you can use the config:cache command.

php artisan config:cache

Clear Laravel View Cache

To clear the cache in Laravel using the PHP Artisan command-line, you can use the view:clear command.

php artisan view:clear

Reoptimized Class

php artisan optimize

Method 2: Laravel Clear Cache Programmatically

If you want to clear the cache through browser then you need to execute these commands programmatically because sometimes hard to get console access to your laravel application. So this method is very easy and helpfull Clear Cache in Laravel using Browser.

Thus, you will create special routes to clear cache in Laravel.

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

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

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

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

If the cache is not cleared yet. Then go to bootstrap/cache directory. And delete all files and sub directories inside the bootstrap/cache directory.

Or, you can execute the following on the terminal instead of manually deleting the file:

cd bootstrap/cache/
rm -rf *.php

Recommended Laravel Tutorials

Recommended:-Laravel Try Catch

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

2 replies to Laravel Clear Cache Using Artisan Command and Programmatically

  1. Thanks for this clear cache in laravel post, I have to clear the cache on shared using this article.

    All the commands for clear cache in laravel ars very useful for me.

    laravel clear cache shared hosting
    laravel clear cache config
    laravel clear cache command line
    laravel clear cache without artisan

    Thanks

  2. All in one

    Terminal:
    php artisan optimize:clear

    Application :

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

Leave a Reply

Your email address will not be published. Required fields are marked *