Get your next remote job on LaraJobs.
1 contributor Edit on GitHub Laravel

How to clear Laravel's cache in a nutshell

Table of contents:

How to clear Laravel's cache in a nutshell

Introduction to clearing Laravel’s cache

When in doubt, clear the cache. In this article, you’ll learn about how to clear every cache Laravel uses.

To clear the cache in Laravel, run php artisan optimize:clear. This works no matter which cache driver you are using. It will also clear the bootstrap files (events, compiled, config, routes, and views).

Now that we got this out of the way, let me remind you that Laravel has many kinds of caches. The framework offers a command for every variety of cache that you can use to enjoy a more granular level of control.

The optimize:clear command in action.

Clear all caches in Laravel

As we saw, the one-stop solution to clear the cache in Laravel is the command php artisan optimize:clear, which clears the following caches:

  • The config cache.
  • The bootstrap cache.
  • The auto-discovered events cache.
  • The application cache.
  • The routes cache.
  • The views cache.

Clear Laravel’s application cache

To clear Laravel’s application cache, run php artisan cache:clear. Whether you are using files, Redis, or memcached, it will be wiped clean.

You can also remove one particular value from the cache using php artisan cache:forget <key> [store]. That’s handy when you’re trying to fix something without disrupting everything else.

And, the cherry on top, you can also clear the cache for a given tag using php artisan cache:clear --tags some-tag,some-other-tag.

Programmatically clear Laravel’s application cache

To programmatically clear Laravel’s application cache, use the Cache facade.

You can forget a given key:

use Illuminate\Support\Facades\Cache;
 
Cache::forget('some-key');

Or flush the cache in its entirety:

use Illuminate\Support\Facades\Cache;
 
Cache::flush();

And, if you don’t want to import one more class, you can use the cache() helper:

cache()->forget('some-key');
cache()->flush();

Clear Laravel’s config cache

To clear Laravel’s config cache, run php artisan config:clear. The bootstrap/cache/config.php file will be deleted, and your fresh config settings will take over.

Clear Laravel’s auto-discovered events cache

To clear Laravel’s auto-discovered events cache, run php artisan event:clear will delete the bootstrap/cache/events.php file. Now Laravel can discover all your shiny new listeners.

Learn more about Laravel’s automatic event discovery.

Clear Laravel’s routes cache

To clear Laravel’s routes caches, run php artisan route:clear. Laravel will remove bootstrap/cache/routes-v7.php. Your new routes are now live and ready to be explored.

Clear Laravel’s scheduled tasks cache

To clear Laravel’s scheduled tasks cache, run php artisan schedule:clear-cache to wipe the slate clean.

Here’s a word of caution: Unless you have good reasons, it’s not advised to run this command in a production environment. Want to know why? Check out Laravel’s guide on preventing task overlaps.

Clear Laravel’s views cache

To clear Laravel’s views cache, run php artisan view:clear. The framework will empty the content of storage/views.

Bonus: turn off Laravel’s application cache

To turn off Laravel’s application cache, change the CACHE_DRIVER environment variable to null.

CACHE_DRIVER=null

This action doesn’t clear the cache but prevents anything from being cached or being retrieved from it.

Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

A complete history of Laravel's versions (2011-2023) A complete history of Laravel's versions (2011-2023)

What's the current version of Laravel? Is there any LTS release you can rely on? And what about the history of the framework? Let's find out!

Modified on Oct 15, 2023

20+ Laravel best practices, tips and tricks to use in 2023 20+ Laravel best practices, tips and tricks to use in 2023

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Modified on Oct 17, 2023

Laravel 10 is out! Here are every new features and changes. Laravel 10 is out! Here are every new features and changes.

Laravel 10 has been released on February 14, 2023. Let's dive into every relevant new features and changes.

Modified on Nov 2, 2023

How does Laravel work? A crystal clear explanation. How does Laravel work? A crystal clear explanation.

Discover my step by step and simple explanation of how Laravel makes your life easier.

Published on Oct 31, 2023

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here

Partners

If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -