How to Clear Cache in Laravel

How to Clear Cache in Laravel

How to Clear Cache in Laravel

Laravel caches various things like configurations, routes, views, and application data to improve performance. Sometimes, you need to clear the cache during development or troubleshooting. Here’s how you can do it:

1. Clear Application Cache

To clear the application cache, run:

php artisan cache:clear

šŸ‘‰ This will remove all cached data stored using cache().

2. Clear Route Cache

If you made changes to your routes and want to refresh them, use:

php artisan route:clear

šŸ‘‰ This will remove the cached route file (bootstrap/cache/routes.php).
šŸ‘‰ If you want to re-cache routes, run:

php artisan route:cache

3. Clear Configuration Cache

Laravel caches configuration files to speed up performance. To clear it, run:

php artisan config:clear

šŸ‘‰ This removes the cached config file (bootstrap/cache/config.php).
šŸ‘‰ To re-cache configuration files, run:

php artisan config:cache

4. Clear View Cache

If Blade templates are not updating, clear the view cache:

php artisan view:clear

šŸ‘‰ This deletes compiled Blade views from storage/framework/views/.
šŸ‘‰ To recompile views, simply refresh your Laravel app in the browser.

5. Clear Event Cache

If you're using event caching, you can clear it with:

php artisan event:clear

šŸ‘‰ To re-cache events, run:

php artisan event:cache

6. Clear All Caches in One Command

To clear everything at once, use:

php artisan optimize:clear

šŸ‘‰ This clears cache, route cache, view cache, config cache, and compiled services.

7. Clear Cache from the Browser (Without SSH)

If you don't have SSH access, you can add this route in web.php and visit it in the browser:

use Illuminate\Support\Facades\Artisan; Route::get('/clear-cache', function () { Artisan::call('optimize:clear'); return 'Cache cleared!'; });

šŸ‘‰ Now visit yourwebsite.com/clear-cache to clear the cache.

8. Clear Specific Cache Keys (Application Cache)

If you want to clear a specific cache entry, use:

Cache::forget('cache_key');

To clear all cache data stored via the cache() helper:

Cache::flush();

Conclusion

Cache TypeCommand
Application Cachephp artisan cache:clear
Route Cachephp artisan route:clear
Config Cachephp artisan config:clear
View Cachephp artisan view:clear
Event Cachephp artisan event:clear
Clear Everythingphp artisan optimize:clear

Now your Laravel cache is cleared! šŸš€ Let me know if you need more help. 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close