Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»PHP Framework»How to Clear Cache in Laravel: Tips and Tricks

    How to Clear Cache in Laravel: Tips and Tricks

    By RahulDecember 21, 20224 Mins Read

    Laravel is an open-source PHP framework used for building modern web applications. It is based on the popular Symphony framework and follows the model–view–controller (MVC) architectural pattern. Caching is a process to store copies of files in temporary storage and serve users on requests. It reduces the request processing time to enhance application performance.

    Advertisement

    This tutorial will help you to clear the cache in the Laravel application. You can clear the Laravel cache by using a command-line interface or by adding the PHP code to the application. Choose any one of the below methods to clear the cache in Larvel.

    Clear Laravel Cache Using PHP Artisan

    Log in to the system running your Laravel application and open a terminal. Then navigate to your Laravel application code. Here you can issue the commands to clear the cache as follows:

    1. Clear All Cache
    2. To clear the cache in Laravel using the PHP Artisan command-line interface, you can use the cache:clear command. This command removes all items from the cache, regardless of the cache driver being used.

      • Here’s an example of how to use the `cache:clear` command to clear the Laravel cache:
        php artisan cache:clear 
        

        When you run the `cache:clear` command, you should see a message indicating that the cache was successfully cleared.

      • If the Laravel application uses multiple caches, then you can also specify the caching store to clear. For example to clear all cache stored from the Redis server, type:
        php artisan cache:clear --store redis 
        
      • You can also clear the specific items from the cache by defining the tags with the command:
        php artisan cache:clear --tags=tag1,tag2 
        

    3. Clear Portion of Cache
    4. Note that the `cache:clear` command is an administrative command, and it is not recommended for use in production environments. If you want to clear only a specific portion of the cache, you can use the `cache:forget` command to delete specific keys instead.

      To use the `cache:forget` command, you can specify the key as an argument. For example:

      php artisan cache:forget key 
      

    5. Clear Route Cache
    6. To clear the route cache of your Laravel application execute the following command from the shell.

      php artisan route:clear
      

    7. Clear Configuration Cache
    8. You can use `config:clear` to clear the config cache of the Laravel application.

      php artisan config:clear
      

    9. Clear Compiled Views Cache
    10. Also, you may need to clear compiled view files of your Laravel application. To clear compiled view files run the following command from the terminal.

      php artisan view:clear
      

    Clear Laravel Cache with PHP Programing

    o clear the cache in Laravel, you can use the Cache facade or the cache helper function. These provide a unified interface for interacting with the various cache drivers, allowing you to store and retrieve data in a consistent manner regardless of the underlying storage mechanism.

    Here’s a step-by-step guide on how to clear the cache in Laravel:

    1. Import the Cache facade at the top of your PHP script.
      1
      use Illuminate\Support\Facades\Cache;
    2. Use the `Cache::flush` method to clear the entire cache. This method removes all keys from the cache, regardless of the cache driver being used.

      1
      Cache::flush();

    3. Use the Cache::forget method to delete a specific key from the cache. This method takes a key as an argument and removes the corresponding key-value pair from the cache.

      1
      Cache::forget('key');

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

    Note that the `Cache::flush` method is an administrative command, and it is not recommended for use in production environments. If you want to clear only a specific portion of the cache, you can use the Cache::forget method to delete specific keys instead.

    Conclusion

    This tutorial describes flushing cache in the Laravel framework using the Artisan command line utility as well as programmatically. Clearing cache in Laravel is safe in all environments. Once the cache is cleared the application starts creating a new cache.

    cache Laravel PHP
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Installing PHP on Pop!_OS

    How to Install PHP 8.x on Pop!_OS

    Managing Dependencies with Composer: A Beginner’s Guide

    Setting up CodeIgniter on Ubuntu: A guide for web developers

    View 22 Comments

    22 Comments

    1. Tu Nguyen on October 27, 2021 4:13 pm

      Thank for helpful!

      Reply
    2. Saeed on January 31, 2021 2:34 pm

      Many Thanks …

      Reply
    3. Iliqn on September 24, 2020 3:29 pm

      Thanks you 🙂 Be safe

      Reply
    4. Walfrido on August 14, 2020 1:21 pm

      Thanks a lot. It’s very useful for me.

      Reply
    5. Yash on August 5, 2020 11:58 am

      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.**

      Reply
    6. Rosen Vlasev on June 16, 2020 12:12 am

      All in one

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

      Reply
    7. Nisha Roy on March 9, 2020 5:31 pm

      Thank you very much. It helps me a lot as a beginner in laravel.

      Reply
    8. shamnas cv on November 22, 2019 6:19 am

      Thanks buddy. Are you a Laravel developer?

      Reply
    9. Anupama on November 15, 2019 2:32 am

      Please change clear route cache from ‘php artisan route:cache ‘ to ‘php artisan route:clear’.

      Thanks,
      Anu

      Reply
      • Rahul on November 15, 2019 9:21 am

        Thanks Anupama, I have corrected the command.

        Reply
    10. prem on October 9, 2019 5:10 am

      Thanks useful

      Reply
    11. Nguyễn Trí Diện on September 5, 2019 4:53 am

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

      Reply
    12. Marco Ruiz on August 8, 2019 8:10 pm

      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?

      Reply
    13. David Wakelin on August 5, 2019 3:48 pm

      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

      Reply
    14. David on July 13, 2019 8:59 am

      thankss for sharing

      Reply
    15. Huntelaer on July 10, 2019 12:28 pm

      In the browser:

      instead of Artisan simply use

      cache()->clear();

      Reply
    16. Larry on May 31, 2019 11:02 pm

      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.

      Reply
    17. hejian on May 1, 2019 1:22 am

      php artisan route:cache ?? clear

      Reply
    18. Jhon Corner on April 26, 2019 12:50 am

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

      Thanks

      Reply
      • Deepak on May 21, 2019 8:32 pm

        php artisan route:cache is to cache the route

        Run
        php artisan route:clear
        php artisan config:clear

        Reply
      • Silvestre on March 14, 2022 7:33 pm

        Hello Jhon Corner I’m having same problem did you find a solution, I relly need the help.

        Reply
    19. Swiya Chatterjee on January 24, 2019 4:41 am

      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.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Split Large Archives in Linux using the Command Line
    • System.out.println() Method in Java: A Beginner’s Guide
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.