
3 minutes read
9 ways to clear Laravel's cache
Table of contents
-
→
TL;DR
- → Quick reference commands
- → What’s actually cached in Laravel?
- → Clear everything (optimize:clear)
- → Clear a specific cache
- → Programmatic clearing (Cache::forget vs flush)
- → One-click route for shared hosting
- → Troubleshooting: what if Artisan commands fail?
- → Performance & deployment best Practices
- → Laravel Version Compatibility
- → FAQ
TL;DR
Run php artisan optimize:clear
during development, but prefer targeted commands in production.
Quick reference commands
- Everything:
php artisan optimize:clear
- Application cache:
php artisan cache:clear
- Specific cache store (Redis):
php artisan cache:clear --store=redis
- Specific cache tags:
php artisan cache:clear --tags=tag1,tag2
- Config cache:
php artisan config:clear
- Routes cache:
php artisan route:clear
- Views cache:
php artisan view:clear
- Events cache:
php artisan event:clear
- Schedule locks:
php artisan schedule:clear-cache
What’s actually cached in Laravel?
Laravel caches several types of data:
- Application Data: Results of database queries or API calls.
- Configuration Files: Cached config files for faster load.
- Routes: Cached routes for quicker route matching.
- Views: Cached compiled Blade templates.
- Events: Cached event listeners.
- Schedule Locks: Prevent task overlaps.
Clear everything (optimize:clear
)
The fastest way to clear every Laravel cache is:
php artisan optimize:clear
This clears configuration, bootstrap files, events, routes, views, and application caches. Ideal during development but avoid in production as it forces Laravel to rebuild caches, temporarily impacting performance.
Clear a specific cache
Application cache
Clear general application cache:
php artisan cache:clear
Clear Redis-specific cache store:
php artisan cache:clear --store=redis
Clear tagged cache items:
php artisan cache:clear --tags=user:123,posts
Configuration cache
Clears cached config files:
php artisan config:clear
Routes cache
Clears cached routes:
php artisan route:clear
Views cache
Clears cached Blade views:
php artisan view:clear
Events cache
Clears cached event listeners:
php artisan event:clear
Schedule locks
Clears schedule lock cache (useful if cron jobs are stuck):
php artisan schedule:clear-cache
Programmatic clearing (Cache::forget
vs flush
)
In your PHP code, clear a specific key:
Cache::forget('key');
Or flush everything (dangerous on production!):
Cache::flush();
Prefer targeted clears (forget
) in production to avoid performance hits.
One-click route for shared hosting
Add this route for easy cache clearing when SSH access is unavailable:
Route::get('/clear-cache', function () { Artisan::call('optimize:clear'); return back()->with('status', 'All caches cleared.'); })->middleware('auth');
Never expose this publicly in production.
Troubleshooting: what if Artisan commands fail?
If Artisan commands themselves throw errors:
- Manually delete files in
bootstrap/cache/*.php
. - Run
composer dump-autoload
to refresh class mappings. - If a deleted provider causes errors, temporarily recreate the provider class, run
config:clear
, then safely remove it again.
Performance & deployment best Practices
- Development: Frequent clearing is okay.
- Production: Rarely clear caches. Prefer cache warming with
config:cache
androute:cache
during deployment. - Never permanently disable caching in production (
CACHE_DRIVER=null
), it’s for debugging only.
Laravel Version Compatibility
Laravel Version | Supported Commands |
---|---|
≤ 8 | Basic commands supported; optimize:clear from 8.24+ |
9–12 | All commands listed above fully supported |
FAQ
Does optimize:clear
delete Redis data?
No. It clears application bootstrap, routes, config, views, and events—not Redis data.
Is Cache::flush()
safe in production?
No. It removes all cached data instantly, potentially causing database spikes.
What changed in Laravel 12 regarding cache? No significant cache command changes. The commands listed here fully apply.
Did you like this article? Then, keep learning:
- Fix common Laravel errors related to cache and transactions
- Learn to fix the frequent "419 Page Expired" error in Laravel
- Discover how to clear Laravel's config files and middleware in Laravel 11
- Understand how Laravel works internally to enhance caching and performance knowledge
- Explore best practices for Laravel including cache usage for 2024
- Discover how to monitor your Laravel apps including cache-related metrics
- Learn about Artisan, Laravel's command interface that includes cache commands
- Explore flexible caching strategies to balance freshness and performance
2 comments
done
That's exactly what this article is meant for, haha!