Benjamin Crozat “Heard about Sevalla? They let you deploy PHP apps with ease.” Claim $50 →

9 ways to clear Laravel's cache

3 minutes read

9 ways to clear Laravel's cache

TL;DR

Run php artisan optimize:clear during development, but prefer targeted commands in production.

The terminal after running “php artisan optimize” to clear Laravel’s cache.

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:

  1. Application Data: Results of database queries or API calls.
  2. Configuration Files: Cached config files for faster load.
  3. Routes: Cached routes for quicker route matching.
  4. Views: Cached compiled Blade templates.
  5. Events: Cached event listeners.
  6. 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:

  1. Manually delete files in bootstrap/cache/*.php.
  2. Run composer dump-autoload to refresh class mappings.
  3. 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 and route: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:

Would you mind helping me reach more people by sharing this article on social media?

2 comments

Guest

Markdown is supported.

Hey, you need to sign in with your GitHub account to comment. Get started →

Great deals for developers