How to Clear Cache in Laravel: Tips and Tricks

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.

This tutorial will help you to understand how to clear the cache in Laravel application. You can clear the Laravel cache by using a command-line interface or by adding the PHP code to your application.

There are 3 methods to clear the cache in the Laravel application given below:

Method 1: Clear Laravel Cache Using PHP Artisan / Command-Line Interface

Step 1: Open the terminal or command prompt in your Laravel application’s root directory.

Step 2: Run the following commands to clear the application cache:

1. Clear Application Cache(Clear All Cache)

php artisan cache:clear

This command will clear the application cache and remove all cached data, including views, routes, and configuration files.

2. Clear Route Cache

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

php artisan route:clear

3. Clear Configuration Cache

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

php artisan config:clear

4. Clear Compiled Views Cache

To clear the view cache of your Laravel application which are basically compiled view files, run the following artisan command:

php artisan view:clear

Method2: Clear Laravel Cache using Browser

Sometimes, it can be difficult to access the console of your Laravel application, especially if you’re using shared hosting. Fortunately, there’s a way to clear the Laravel cache through a web browser, which can be more convenient.

Here’s how you can clear the Laravel cache through a web browser:

Step 1: Create a new route in your Laravel application by adding the following code to your routes/web.php file:

// Clear application cache
Route::get('/clear-cache', function() {
    $exitCode = Artisan::call('cache:clear');
    return 'Application cache has been cleared';
});

Step 2: Save the file and upload it to your server.

Step 3: Open your web browser and navigate to the URL of the route you just created. For example, if you uploaded the file to the root directory of your application, the URL would be:

http://your-app.com/clear-cache

Step 4: Hit Enter, and the Laravel cache will be cleared.

// Clear application cache:
 Route::get('/clear-cache', function() {
     $exitCode = Artisan::call('cache:clear');
     return 'Application cache has been cleared';
 });

//Clear route cache:
 Route::get('/route-cache', function() {
     $exitCode = Artisan::call('route:cache');
     return 'Routes cache has been cleared';
 });

 //Clear config cache:
 Route::get('/config-cache', function() {
     $exitCode = Artisan::call('config:cache');
     return 'Config cache has been cleared';
 }); 

 // Clear view cache:
 Route::get('/view-clear', function() {
     $exitCode = Artisan::call('view:clear');
     return 'View cache has been cleared';
 });

Method3: Clear Laravel Cache with PHP Programming

To clear cache in Laravel using PHP, 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 is the step-by-step guide on how to clear the cache in Laravel using the Cache facade or the cache helper function:

Step 1: Import the Cache facade at the top of your PHP script.

use Illuminate\Support\Facades\Cache;

Step 2: If you’re using the Cache facade, add the following code to your method or closure:

Cache::flush();

This will clear all items from the cache regardless of the cache driver being used.

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.

Cache::forget('key');

Step 3: If you’re using the cache helper function, add the following code to your method or closure:

cache()->flush();

This will also clear all items from the cache.

Step 5: Save your changes and test your application to make sure the cache is cleared.

That’s it! You’ve successfully cleared the cache in Laravel using the Cache facade or the cache helper function. This method provides a simple and consistent way to interact with the cache, regardless of the underlying storage mechanism.

Leave a Comment