PHP Artisan Clear Cache

Sheeraz Gul Sep 26, 2022
PHP Artisan Clear Cache

This tutorial demonstrates how to clear cache using artisan in Laravel PHP.

PHP Artisan Clear Cache

The artisan is a command line interface provided in the Laravel framework of PHP. The artisan is located at the root of our application and provides many commands that can help us build our application.

The artisan also provides different commands to clear the cache memory of Laravel because it has different caches for different parts. These caches can be cleared in multiple ways.

PHP Laravel - Clear Primary Cache

The primary cache for Laravel is the application cache which will store everything cached in the application. To clear this cache, we use the command:

php artisan cache:clear

The above command will clear the cache for the application.

PHP Laravel - Clear Specific Cache With/Without Tags

If you are using multiple caches and you want to remove a specific cache, then we need to pass that as a parameter; use the following command:

php artisan cache:clear --store=redis

The above command will clear the cache for the redis part. We can also use tags to clear only the specific elements from the cache; for that, we need to pass the tags as parameters:

php artisan cache:clear --tags=tag_1,tag_2

PHP Laravel - Use Artisan Command to Clear Cache

We can also use the artisan command outside the CLI or command prompt; then, we need to use it in the code. Here is an example:

Route::get('/clear-cache', function() {
    $Exit_Code = Artisan::call('cache:clear');
    // Your code here, return anything
})

The code above shows how to run an artisan command in the PHP code. The artisan doesn’t only provides the functionality to clear the cache; it also offers the following commands for other types of caches:

  1. php artisan view:cache - This command is used for the view cache, where our application stores rendered blade templates to speed up our application. To clear this cache, we use the command:

    php artisan view:clear
    
  2. php artisan config:cache - The config cache is recommended by Laravel because, with this, the application doesn’t have to go and check the config files again and again. The config cache is cleared when we change the configuration; see the command:

    php artisan config:clear
    
  3. php artisan event:cache - The event cache is done for efficient event handling while running in production. The event cache is done during the deployment process, this command also clears all the previous event caches automatically, but if we have to do it manually, then we use:

    php artisan event:clear
    
  4. php artisan route:cache - The route cache is considered the additional performance cache, which is part of the deployment process. This cache helps us to decrease the time when registering the application routes. To clear this cache, the command is:

    php artisan route:clear
    
  5. php artisan optimize:clear - This command is used to clear all the caches from Laravel. For example, the command will clear Compiled views, Application cache, View cache, Event cache, Route cache, and Compiled services and packages.

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook