A brief guide on clearing Laravel’s cache

Posted by

Introduction to clearing Laravelā€™s cache

Cache clearing is advised when unsure. You will discover how to empty each and every cache that Laravel uses in this tutorial.

In Laravel, use php artisan optimize: clear to clear the cache. Regardless of the cache driver you are using, this works. The bootstrap files (events, compiled, config, routes, and views) will also be cleared.

Having cleared this up, let me to remind you that Laravel offers a variety of cache types. To enjoy a more granular level of control, the framework provides a command for each cache type.

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.

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
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x