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

Wakanda's avatar
Level 10

Disable artisan commands

Hi Devs, is there a way to disable the artisan command. For example in the production environment, I never want to run PHP artisan migrate:fresh.

0 likes
14 replies
Wakanda's avatar
Level 10

@martinbean Thanks for your reply that would work but would still need access to commands for caching, crone, sitemap, and other custom commands

laracoft's avatar
laracoft
Best Answer
Level 27

@wakanda

// register in your ServiceProvider's boot()
\Event::listen(
    \Illuminate\Console\Events\CommandStarting::class,
    \Your\Namespace\PreCommand::class
);
namespace Your\Namespace;

use Illuminate\Console\Command;
use Illuminate\Console\Events\CommandStarting;

class PreCommand extends Command
{
    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(CommandStarting $event): void
    {
        if (app()->environment(['production'])) {
            if ($event->command == 'migrate:fresh') {
                $this->output = $event->output;
                $this->info('Nope. Not running.');
                die();
            }
        }
    }
}
3 likes
martinbean's avatar

@wakanda You can’t have it both ways. You either want Artisan on the server or you don’t.

laracoft's avatar

2nd is a command file, just put it anywhere as long as namespace mtaches your folder.

2 likes
Snapey's avatar

Don't understand the problem. Artisan Migrate prompts when in production, and should cause no issue if your database has already been migrated.

Wakanda's avatar
Level 10

@snapey I know it prompts when in production, but my case is that in the production environment I never want to run php artisan migrate:fresh. So prompting does not mean it cannot run it completely. So just adding an extra protection layer in case of human error.

And to be explicit am worried about migrate:fresh rollback and refresh commands, so disabling them helps me better sleep at night.

Wakanda's avatar
Level 10

@snapey thanks, maybe i need to change my backups from weekly to daily

martinbean's avatar

And to be explicit am worried about migrate:fresh rollback and refresh commands, so disabling them helps me better sleep at night.

@wakanda Well who’s SSH-ing into your production server and running migrate:fresh?

emjones's avatar

@martinbean I have a scenario that I'd like you to consider:

  • I'm using a container orchestration platform to deploy my Laravel applications
  • i'd like to leverage the same Laravel health APIs from my command line that I am for monitoring my server health such that the container orchestrator can do health/liveness probes during start-up
  • I don't want artisan to be usable for other scenarios.

Would this be a valid reason to want to have artisan on my server without it being usable for other use cases?

https://github.com/spatie/laravel-health for reference

Please or to participate in this conversation.