Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dmag's avatar
Level 6

Should I also add `optimize:clear` to my deployment script?

In my deployment script (forge), should I run php artisan optimize:clear before I run php artisan optimize or just running php artisan optimize is the way to go?

0 likes
4 replies
LaryAI's avatar
Level 58

When deploying a Laravel application, it's generally a good practice to ensure that any cached data from previous deployments is cleared to avoid potential issues. The php artisan optimize:clear command clears all the caches, including route, config, and view caches, which ensures that your application starts with a clean slate.

Here is a recommended approach for your deployment script:

  1. Run php artisan optimize:clear to clear all the caches.
  2. Run php artisan optimize to re-optimize the application by caching the routes, config, and views.

This ensures that any changes made to your configuration, routes, or views are properly reflected in the optimized caches.

Here is an example of how you can structure your deployment script:

# Clear all caches
php artisan optimize:clear

# Re-optimize the application
php artisan optimize

By following this approach, you ensure that your application is always running with the latest optimized configurations and resources, reducing the risk of encountering issues related to stale cache data.

1 like
karim_aouaouda's avatar

as the AI montioned, the two commands are different ,

php artisan optimize:clear

will remove and delete cache files when

php artisan optimize

will re-optimize the application with creating a new cache files, so to avoid any conflits, it's good to clear the cache files before re-optimize the app before deployement

dmag's avatar
Level 6

@karim_aouaouda yep, but should I run optimize:clear before I run optimize in my deployment script?

karim_aouaouda's avatar
Level 1

@Nezo96-39273050 after degging deeper inside laravel command system, i will tell you that you don't need to call

php artisan optimize:clear

because it will be called when you run :

php artisan optimize

look : in OptimiseCommand class (which is responsable of executing optimize command) it will call the cache commands for config, routes ... :

public function handle()
    {
        $this->components->info('Caching framework bootstrap, configuration, and metadata.');

        collect([
            'config' => fn () => $this->callSilent('config:cache') == 0,
            'events' => fn () => $this->callSilent('event:cache') == 0,
            'routes' => fn () => $this->callSilent('route:cache') == 0,
            'views' => fn () => $this->callSilent('view:cache') == 0,
        ])->each(fn ($task, $description) => $this->components->task($description, $task));

        $this->newLine();
    }

and each of these command will delete the old ones , for example let's look at config:cache at ConfigCacheCommand class it will calll config:clear before cache the new ones :

public function handle()
    {
        $this->callSilent('config:clear');

        $config = $this->getFreshConfiguration();
		...
		...
	}

i wish this response is help you :)

i found these in ArtisanServiceProvider :)

3 likes

Please or to participate in this conversation.