If you are working in Laravel web application and you are facing any error due to cache in your web application or some things are not working due to cache in laravel web apps. So In this tutorial, you will learn how to clear route, config, view, and app cache in laravel 10, 9, 8 apps using artisan command and without command.
How to Clear Cache In Laravel 10, 9, 8?
By using the following two methods to clear route, app, config, view cache in Laravel 10, 9, 8 apps:
- Solution 1 – Laravel Clear route, app, config, view cache using Command Line
- Solution 2 – Laravel Cache Clear using without Command Line
Solution 1 – Laravel Clear route, app, config, view cache using Command Line
Let’s explore the various PHP artisan commands available in Laravel 10, 9, and 8 to delete or clear route, app, config, and view caches:
- Laravel Clear Route Cache
- Laravel Clear App Cache
- Laravel Clear Config Cache
- Laravel Clear View Cache
- Laravel Clear Cache using Reoptimized Class
Laravel Clear Route Cache
Use the below command and clear your routes cache :
php artisan route:cache
Laravel Clear App Cache
Use the below command and clear your application cache like session cache, cookies cache:
php artisan cache:clear
Laravel Clear Config Cache
Use the below command and clear your config cache :
php artisan config:cache
Laravel Clear View Cache
Use the below command and clear your view (blade) cache :
php artisan view:clear
Laravel Clear Cache using Reoptimized Class
php artisan optimize
Solution 2 – Laravel Cache Clear using without Command
If you are unable to access SSH on shared hosting servers, there is an alternative method to clear the cache by modifying the route file. To do this, follow the steps below:
- Locate the
web.php
file in your project. This file is usually located in theroutes
directory. - Open the
web.php
file in a text editor or an integrated development environment (IDE). - Insert the following code snippet at the desired location within the file:
//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';
});
Then save the changes to the web.php
file.
Now, you can clear the cache of your application by accessing the /clear-cache
URL in your web browser. For example, if your website URL is http://example.com
, you would visit http://example.com/clear-cache
.
After accessing the URL, the cache of your application will be cleared, and you should see a message indicating successful cache clearance.
If the cache is not cleared yet. Then go to the 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