Support the ongoing development of Laravel.io →
Article Hero Image

Why Laravel uses caches and how to clear them for debugging

10 Sep, 2022 2 min read

Photo by Mitchell Luo on Unsplash

Some performance issues at scale can come from repeatedly computing values for Laravel's bootstrapping process, which occurs at each request your visitors make (unless you're using Octane). Therefore, caching those values is essential to help your application stays fast. Sometimes though, problems happen, and cache gets in the way of debugging. Let's see how we can clear every cache Laravel uses.

This is an article originally posted on Benjamin Crozat's blog.

Why and how to clear config cache in Laravel

Some config values are fetched from your environment file and it can be a bit slow. Luckily, Laravel can cache them to help us speed up our applications.

php artisan config:clear

Why and how to clear events cache in Laravel

Laravel's automatic event discovery is beneficial. You don't need to register listeners manually anymore thanks to this tiny change you can make in your EventServiceProvider.

…

class EventServiceProvider extends ServiceProvider
{
    …

    public function shouldDiscoverEvents() : bool
    {
        return true;
    }
}

When going into production, you can cache auto-discovered events for maximum performance.

php artisan event:clear

Why and how to clear routes cache in Laravel

Routes are an essential part of your web application or API. Resolving a route can take time if you have a lot of them and as you guessed, caching helps for that.

php artisan route:clear

Why and how to clear scheduled tasks cache in Laravel

Let's say you have a recurring task that takes so much time to complete it will overlap with its next occurrence. You can prevent it until the previous one has finished:

$schedule->command('some:task')->withoutOverlapping();

Behind the scenes, Laravel uses the application's cache to remember which task hasn't finished running.

php artisan schedule:clear-cache

Why and how to clear views cache in Laravel

Blade directives are compiled and cached even in your local environment. Sometimes though, compiled views can conflict with a recent change in your code. Again, you have a command for that:

php artisan view:clear

How to clear every cache in Laravel

Finally, let's see the ultimate cache-busting command.

php artisan optimize:clear

This command will remove the following caches:

  • Config
  • Compiled classes cache
  • Events
  • General Cache
  • Routes
  • Views

How do I know that? Simple. I used the "Go To File" command in my code editor and searched for the "OptimizeClearCommand.php" file. Its source code is straightforward to understand, as you can see:

…

class OptimizeClearCommand extends Command
{
    …

    public function handle()
    {
        $this->components->info('Clearing cached bootstrap files.');

        collect([
            'events' => fn () => $this->callSilent('event:clear') == 0,
            'views' => fn () => $this->callSilent('view:clear') == 0,
            'cache' => fn () => $this->callSilent('cache:clear') == 0,
            'route' => fn () => $this->callSilent('route:clear') == 0,
            'config' => fn () => $this->callSilent('config:clear') == 0,
            'compiled' => fn () => $this->callSilent('clear-compiled') == 0,
        ])->each(fn ($task, $description) => $this->components->task($description, $task));

        $this->newLine();
    }
}
Last updated 1 month ago.

benjamincrozat liked this article

1
Like this article? Let the author know and give them a clap!
benjamincrozat (Benjamin Crozat) Full-stack Laravel developer passionate about the TALL stack. Obsessed with SEO. Big appetite for UI and UX design.

Other articles you might like

Article Hero Image December 13th 2024

How to add WebAuthn Passkeys To Backpack Admin Panel

Want to make your Laravel Backpack admin panel more secure with a unique login experience for your a...

Read article
Article Hero Image December 13th 2024

Quickest way to setup PHP Environment (Laravel Herd + MySql)

Setting up a local development environment can be a time taking hassle—whether it's using Docker or...

Read article
Article Hero Image December 5th 2024

How to set up Laravel Magic Link?

User authentication is crucial for making web applications secure and easy to use. Traditionally, pa...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.