Skip to content

How to Clear Cache in Laravel: Tips and Tricks

Advertisement

In the fast world of making websites, being quick and efficient is very important. Laravel is a popular PHP framework because it has easy-to-understand tools and syntax. One important part of keeping a Laravel app running well is managing the cache. Caching helps make pages load faster by storing a copy of the page, so the server doesn’t have to process everything again for each request. But to make sure users get the latest content, it’s important to know how to clear the cache properly.

This guide will show you how to clear cache in Laravel easily, ensuring your apps run smoothly and quickly.

Laravel’s Caching Mechanisms

Before we get into how to clear the cache, let’s understand the different types of caching in Laravel:

  • Route Cache: Caches the app’s routes to make route registration faster.
  • Config Cache: Caches configuration files to reduce the time needed to load these files on each request.
  • View Cache: Caches compiled views to avoid recompiling templates every time.
  • Application Cache: Caches data like query results, objects, or arrays to make the app faster.

Clearing Laravel Cache Using PHP Artisan

Log in to the system where your Laravel app is running and open a terminal. Go to your Laravel app’s code folder. You can use the following commands to clear the cache:

  1. Clear All Cache
  2. To clear all cache in Laravel, use the cache:clear command. This removes all items from the cache.

    • Example command:
      php artisan cache:clear
      

      You will see a message saying the cache was successfully cleared.

    • If your app uses multiple caches, you can specify which one to clear. For example, to clear cache stored on a Redis server, use:
      php artisan cache:clear --store redis
      
    • You can also clear specific items from the cache by using tags:
      php artisan cache:clear --tags=tag1,tag2
      

  3. Clear Portion of Cache
  4. Using cache:clear is not recommended in production. To clear specific parts of the cache, use cache:forget to delete specific keys.
    Example command:

    php artisan cache:forget key
    

  5. Clear Route Cache
  6. To clear the route cache, use:

    php artisan route:clear
    

  7. Clear Configuration Cache
  8. To clear the config cache, use:

    php artisan config:clear
    

  9. Clear Compiled Views Cache
  10. To clear compiled view files, use:

    php artisan view:clear
    

Clear Laravel Cache with PHP Programming

You can also clear the cache using PHP code. The Cache facade or cache helper function lets you interact with various cache drivers consistently.

Here’s how to do it:

  1. Import the Cache facade at the top of your PHP script:
    
    use Illuminate\Support\Facades\Cache;
    
    
  2. Use the Cache::flush method to clear the entire cache:
    
    Cache::flush();
    
    
  3. Use the Cache::forget method to delete a specific key from the cache:
    
    Cache::forget('key');
    
    

That’s it! The cache is now cleared, and all keys have been removed.

Note: The Cache::flush method is an administrative command and not recommended in production. Use Cache::forget to delete specific keys instead.

Conclusion

Managing cache is crucial for high-performing Laravel apps. By using Laravel’s caching mechanisms and knowing how to clear the cache, developers can keep their apps efficient, responsive, and up-to-date. Caching is powerful but requires careful management to avoid serving old content or causing performance issues.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

22 Comments

  1. The question is pretty clear.

    php artisan cache:clear
    Is there any workaround to clear the cache like above we using in CLI. I am using a popular shared hosting service, but as per my plan, I don’t have control panel access.

    ** I want to clear the views cache.**

  2. All in one

    Route::get(‘/clear-cache’, function() {
    Artisan::call(‘optimize:clear’);
    echo Artisan::output();
    });

  3. i readed your infomation and I am very impressed for you. Thanks you for your sharing. It is very useful !

  4. I have the same problem.
    Calling / clear-cache is enough for all clients to see the same page, or do we have to make the call on each client computer?

  5. Please fix this error in your post:
    php artisan config:cache does not clear your cache. This will cause problems with unit tests.
    It should be php artisan config:clear

  6. IMPORTANT! #3 should be:

    php artisan config:clear

    NOT config:cache

    Hey, Rahul, would you mind updating the article? I spent a bunch of time debugging a problem with env() after running config:cache. Your article has a high Google result for “laravel clear cache” and it would be nice for the information to be correct.

  7. I ran the commands and the laravel site stop working: appear 404
    Not Found. Any help will be appreciate.

    Thanks

  8. Laravel provides an expressive, unified API for various caching backends. Also, Laravel is configured to use the file cache driver, which stores the serialized, cached objects in the filesystem. I would like to add that the above-listed steps to clear cache are easy to follow and important too for Laravel Application Development.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top