Skip to content

Prohibit destructive commands in production

At the end, destructive module commands refuse to run in production, even when passed --force.

  1. Call Module::prohibitDestructiveCommands() in the boot() method of an application service provider. Pass $this->app->isProduction() to block them only in production, or omit the argument to block them in every environment.

    app/Providers/AppServiceProvider.php
    use Nwidart\Modules\Facades\Module;
    public function boot(): void
    {
    Module::prohibitDestructiveCommands($this->app->isProduction());
    }
  2. Deploy the change to production. When APP_ENV is production, Laravel applies the prohibition during boot.

Module::prohibitDestructiveCommands() blocks five commands:

  • module:migrate-fresh
  • module:migrate-refresh
  • module:migrate-reset
  • module:migrate-rollback
  • module:delete

Each command has its own prohibition flag, so you can also block one command on its own (for example MigrateFreshCommand::prohibit()). See Destructive commands.

Other commands stay unaffected. In production, module:migrate and module:seed still require Laravel’s confirmation or --force.

A blocked command prints a warning, runs nothing, and exits with status 1:

WARN This command is prohibited from running in this environment.

--force skips confirmation prompts only; it does not override a prohibition. To allow an emergency run, gate the prohibition on an environment variable:

app/Providers/AppServiceProvider.php
Module::prohibitDestructiveCommands(
$this->app->isProduction() && ! env('ALLOW_DESTRUCTIVE_COMMANDS', false)
);

Pass the variable in the process environment when running the command:

Terminal window
ALLOW_DESTRUCTIVE_COMMANDS=true php artisan module:migrate-rollback Blog --force

Setting the variable in .env has no effect when configuration is cached.

Without prohibition, the four destructive migration commands prompt for confirmation in production unless you pass --force:

Terminal window
php artisan module:migrate-fresh --all --force

module:delete prompts in every environment unless you pass --force.

In the production environment, run:

Terminal window
php artisan module:migrate-rollback Blog --force

The command prints This command is prohibited from running in this environment. and exits with status 1.