How to clear cache in Laravel 5

Introduction

Clear cache in laravel means you will have noticed sometimes when you code it does not reflect on the web browser because of Laravel is serving pages on the cache. In this tutorial, we will learn about how to clear cache and optimize the framework bootstrap files in Laravel project.

Let's get started

Clear cache in Laravel via terminal or command prompt

The following are the commands which will be used to clear the different cache files from your Laravel application. To run the command navigate to your application root directory and enter the below artisan commands.

 

# Clear Laravel application cache

To flush Laravel application cache enter the following command.

php artisan cache:clear

# Clear route cache

Enter the following command will remove the routes cache file.

php artisan route:clear 

# Clear view cache

When you create a new blade file. Before serving laravel automatically compile your view file. Enter the following command to clear all compiled view cache files

php artisan view:clear

# Clear config cache

To remove all the configuration cache enter the following command

php artisan config:clear
 

Clear cache in Laravel from your shared hosting

When you deploy your Laravel project on shared hosting you don't get ssh access to your server means you can't run artisan command on your shared hosting server. For this case, we will define routes and call the artisan command to clear all the cache from the web browser.

Edit your web.php file and add the below code

#routes/web.php

// clear application cache
Route::get('/clear-cache', function() {
    Artisan::call('cache:clear');
    return "Application cache flushed";
});

// clear route cache
Route::get('/clear-route-cache', function() {
    Artisan::call('route:clear');
    return "Route cache file removed";
});

// clear view compiled files
Route::get('/clear-view-compiled-cache', function() {
    Artisan::call('view:clear');
    return "View compiled files removed";
});

// clear config files
Route::get('/clear-config-cache', function() {
    Artisan::call('config:clear');
    return "Configuration cache file removed";
});