How to delete all the rows in a table using Eloquent?

My guess was to use the following syntax:

MyModel::all()->delete();

But that did not work. I'm sure it's super simple, but I've searched for documentation on the subject and can't find it!


Solution 1:

The reason MyModel::all()->delete() doesn't work is because all() actually fires off the query and returns a collection of Eloquent objects.

You can make use of the truncate method, this works for Laravel 4 and 5:

MyModel::truncate();

That drops all rows from the table without logging individual row deletions.

Solution 2:

Laravel 5.2+ solution.

Model::getQuery()->delete();

Just grab underlying builder with table name and do whatever. Couldn't be any tidier than that.

Laravel 5.6 solution

\App\Model::query()->delete();