Maximize your Laravel blog's potential with SEO best practices and reach 10K monthly clicks on Google.
Preview the course for free
Benjamin Crozat New!
Benjamin Crozat The art of crafting web applications

Laravel: clear the cache like a pro

Benjamin Crozat — Updated on
Artboard

Hundreds of developers subscribed to my newsletter.
Join them and enjoy free content about the art of crafting websites!

Powered by

Table of contents

Laravel: clear the cache like a pro

How to clear the cache

To clear the cache in Laravel, use the php artisan optimize:clear command.

Laravel will remove the config cache, the bootstrap cache, the events cache, the application cache, the routes cache and the views cache.

php artisan optimize:clear

All of these can drastically speed up your applications. But sometimes, it comes in the way of our debugging duties.

Let’s review even more commands to help you have a more granular control over it.

How to clear the application cache (memcached, Redis)

To clear the application cache in Laravel, use the php artisan cache:clear command.

Depending on your cache driver (defined in your .env file and named CACHE_DRIVER), Laravel will clear files on disk or data in Redis or memcached.

Now, if you just need to remove one value from the cache, the php artisan cache:forget <key> [store] does exactly that.

php artisan cache:clear

How to clear config cache

To clear the config cache in Laravel, use the php artisan config:clear command.

Laravel will clear the cache by deleting bootstrap/cache/config.php.

If you previously ran php artisan config:cache, you can now update values in .env file and see the changes when you run your code. 👍

php artisan config:clear

How to clear events cache

To clear the events cache in Laravel, use the php artisan event:clear command.

If you previously ran php artisan event:cache, Laravel will delete bootstrap/cache/events.php and discover all the new listeners you added to your codebase.

Learn more about Laravel’s automatic event discovery.

php artisan event:clear

How to clear routes cache

To clear the routes cache in Laravel, use the php artisan route:clear command.

If you previously ran php artisan route:cache, Laravel will remove bootstrap/cache/routes-v7.php and you will be able to see all the new routes you registered using php artisan route:list.

php artisan route:clear

How to clear scheduled tasks cache

To clear the scheduled tasks cache, use the php artisan schedule:clear-cache command.

Learn more about Laravel’s tasks overlap prevention, because that’s what this cache is for.

Unless you have good reasons, I discourage you to run this command in production.

How to clear views cache

To clear the views cache, use the php artisan view:clear command.

Laravel will delete the content of storage/views.

The views cache is generated every time you hit your route unless it already exists and your view didn’t change. You shouldn’t have much need for this command.

php artisan view:clear

Bonus: turn off the application cache

To completely turn off the application cache, change the cache driver to null in your .env file.

CACHE_DRIVER=null
Recommended

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.

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

I show you how to upgrade your Laravel 9 project to version 10 and help you decide whether the return on investment is worth it.

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

I show you how to upgrade your Laravel 8 project to version 9 and help you decide whether the return on investment is worth it.

Here's a case study for my blog in the programming niche, where I share everything I did to increase clicks by a huge amount since the beginning.

Migrations are essential in any Laravel app using a database. I will tell you what they are, why you should use them and how you can generate them.

Knowing which Laravel version you are running is important before you start writing code on a new project. There are multiple ways to do so.

Store and manage files on Dropbox and use it to back up your Laravel app automatically. Compatible with PHP 8.1+ and Laravel 9+.

Start leveraging the power of AI today. It enables developers to do incredible things, and many startups build products around it.

Powered by