How to clear the Laravel cache?

RMAG news

Laravel, a popular PHP web application framework, offers powerful caching mechanisms to enhance the performance of your application. However, there are situations where you might need to clear the cache, especially during development or after making configuration changes. In this guide, we’ll explore the step-by-step process of clearing various caches in Laravel

1. Configuration Cache:
One of the primary caches you might want to clear is the configuration cache. This cache stores the configuration files in a compiled and optimized form. To clear it, open your terminal and run the following Artisan command:
php artisan config:cache
This command will clear the configuration cache and recompile the configuration files, ensuring that any changes you’ve made take effect.

2. Route Cache:
Routing is a critical aspect of any web application, and Laravel provides a route caching mechanism for faster performance. To clear the route cache, use the following Artisan command:
php artisan route:cache
This command will generate a new cached file for your routes, improving the efficiency of route registration.

3. View Cache:
If you are using view caching for faster rendering of views, you can clear the view cache with the following command:
php artisan view:clear
This command will delete all compiled view files, allowing Laravel to recompile them as needed.
4. Compiled Classes:
Laravel compiles various classes and services for optimization. To clear these compiled classes, use the clear-compiled Artisan command:
php artisan clear-compiled
This command removes the compiled classes and services file, ensuring a clean slate for your application.
5. Clear All Caches:
For a comprehensive cache clearance, you can use the cache:clear Artisan command:
php artisan cache:clear
This command clears the entire cache, including the configuration, routes, and any other cached data.
6. Clear Configuration and Route Cache in One Command:
To clear both the configuration and route cache in a single command, use:
php artisan optimize
This command not only clears the configuration cache but also generates a new optimized class loader.

In scenarios where SSH access is not available on shared hosting servers, clearing the Laravel cache requires an alternative approach. In such cases, we can utilize the routes defined in our application’s routes/web.php file to trigger the necessary Laravel clear cache commands. This enables the clearance of Laravel cache by accessing specific routes through a web browser. This method offers a practical solution for shared hosting environments where direct server access via SSH is restricted.

//— Clear route cache
Route::get(‘/route-cache’, function() {
Artisan::call(‘route:cache’);
return ‘Routes cache cleared’;
});

//— Clear config cache
Route::get(‘/config-cache’, function() {
Artisan::call(‘config:cache’);
return ‘Config cache cleared’;
});

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

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

//— Clear cache using reoptimized class
Route::get(‘/optimize-clear’, function() {
Artisan::call(‘optimize:clear’);
return ‘View cache cleared’;
});

Please follow and like us:
Pin Share