clear laravel cache from browser

Laravel provides in-built functionality for caching. Cache is used for better application performance. Laravel has predefined function for handling cache operations like creating retrieving and deleting cache. Before stating let's understand cache :

What is cache?

A cache is a special storage space for temporary files that makes a device, browser, or app run faster and more efficiently. When user opens application first time then Laravel creates cache automatically.

Why cache Used?

Generally Cache is used to increase data retrieval performance by reducing the need to access the underlying slower storage layer. Cache can also used for storing temporary data.


As mentioned Laravel Framework automatically generate cache files. But Sometimes, we need to clear those cache file.

For example we have home page of our application and used it for while but later we made some other changes. In application cache files old version of you home page is stored whenever existing user try to access that page their are chances for showing old page. This problem can solve by clearing view cache files.

In Laravel we can clear caches by two methods :

  • Using Terminal
  • Using Browser
  • Let's take examples for them one by one :

    Clear Cache in Laravel Application using Terminal

    For this method you need to navigate to root directory and enter those commands to clear cache as per type.

    Clear Application Cache

    To clear Laravel application cache, run the following artisan command:

        
    php artisan cache:clear
        
    

    Clear Route Cache

    Laravel automatically cache all routes but when you make changes to route. you will face error while accessing new page. To clear route cache enter below command:

        
    php artisan route:clear
        
    

    Clear Configuration Cache

    Below Command will clear configuration cache which contains application configuration like, database configuration, mail configuration, queue configuration, view configuration and much more.

        
    php artisan config:clear
        
    

    Clear View Cache

    Compiling views during the request may have a small negative impact on performance, so Laravel provides the view:cache Artisan command to precompile all of the views utilized by your application. You can clear view cache by below command :

        
    php artisan view:clear
        
    

    Clear All Cache at Once

    Laravel has wildcard command to clear all type of caches like application, route, views, configuration and files.

        
    php artisan optimize:clear
        
    

    Clear Cache in Laravel Application using Browser

    Sometime your hosting service provider/plan doesn't provides terminal or SSH access. Then we can user this method to clear cache files using browser.

    As you know, you can run any console command using Artisan Facade in Laravel. So we can use routing for run those command programmatically. We just need to define routes for this task. Here is example of sample route to clear caches by it's type:

    routes\web.php

        
    //To Clear application cache:
    Route::get('/clear-cache', function() {
        $exitCode = Artisan::call('cache:clear');
        return 'Application cache has been cleared';
    });
    
    //To Clear route cache:
    Route::get('/route-cache', function() {
        $exitCode = Artisan::call('route:cache');
        return 'Routes cache has been cleared';
    });
    
    //To Clear config cache:
    Route::get('/config-cache', function() {
        $exitCode = Artisan::call('config:cache');
        return 'Config cache has been cleared';
    }); 
    
    //To Clear view cache
    Route::get('/view-clear', function() {
        $exitCode = Artisan::call('view:clear');
        return 'View cache has been cleared';
    });
    
    // Optimize Clear Method
    Route::get('/optimize:clear', function() {
        $exitCode = Artisan::call('optimize:clear');
        return 'All caches clear';
    });
        
    

    Share your thoughts

    Ask anything about this examples