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

AlexGodbehere79's avatar

How to truncate laravel table?

Hello, I want to truncate laravel model on the console command. Is there any easy way to do that? Thank you in advanced.

0 likes
8 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@alexgodbehere79 I assume you want to truncate a database table. In that case, you can try-

Model::query()->truncate();

Note: Plz replace the word Model with your model name.

7 likes
internetbug256's avatar

Anyone knows why Model::truncate() wouldn't work on a script, but does work from Tinker? Believe it or not, it happens to me.

1 like
internetbug256's avatar

@JussiMannisto I mean that I put that sentence inside some controller, and seems to execute, but NOT truncating the table. However, when I run it from Tinker, it does clear the table.

djjox's avatar

Run the command:

php artisan make:command TruncateTable

Open app/Console/Commands/TruncateTable.php and update it like this:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class TruncateTable extends Command
{
    protected $signature = 'table:truncate {table}';
    protected $description = 'Truncate a specific MySQL table';

    public function handle()
    {
        $table = $this->argument('table');

        // Disable foreign key checks temporarily
        DB::statement('SET FOREIGN_KEY_CHECKS=0;');

        // Truncate the table
        DB::table($table)->truncate();

        // Re-enable foreign key checks
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

        $this->info("Table '{$table}' has been truncated.");
    }
}

Now you can run your command like this:

php artisan table:truncate your_table_name

Replace your_table_name with the actual name of your MySQL table.

Please or to participate in this conversation.