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

LaravelLaravel 4Eloquent

Laravel Problem Overview


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!

Laravel Solutions


Solution 1 - Laravel

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

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();

Solution 3 - Laravel

You can use Model::truncate() if you disable foreign_key_checks (I assume you use MySQL).

DB::statement("SET foreign_key_checks=0");
Model::truncate();
DB::statement("SET foreign_key_checks=1");

Solution 4 - Laravel

I've seen both methods been used in seed files.

// Uncomment the below to wipe the table clean before populating

DB::table('table_name')->truncate();

//or

DB::table('table_name')->delete();

Even though you can not use the first one if you want to set foreign keys.

> Cannot truncate a table referenced in a foreign key constraint

So it might be a good idea to use the second one.

Solution 5 - Laravel

There is an indirect way:

myModel:where('anyColumnName', 'like', '%%')->delete();

Example:

User:where('id', 'like' '%%')->delete();

Laravel query builder information: https://laravel.com/docs/5.4/queries

Solution 6 - Laravel

I wanted to add another option for those getting to this thread via Google. I needed to accomplish this, but wanted to retain my auto-increment value which truncate() resets. I also didn't want to use DB:: anything because I wanted to operate directly off of the model object. So, I went with this:

Model::whereNotNull('id')->delete();

Obviously the column will have to actually exists, but in a standard, out-of-the-box Eloquent model, the id column exists and is never null. I don't know if this is the best choice, but it works for my purposes.

Solution 7 - Laravel

I wasn't able to use Model::truncate() as it would error:

> SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint

And unfortunately Model::delete() doesn't work (at least in Laravel 5.0):

> Non-static method Illuminate\Database\Eloquent\Model::delete() should not be called statically, assuming $this from incompatible context

But this does work:

(new Model)->newQuery()->delete()

That will soft-delete all rows, if you have soft-delete set up. To fully delete all rows including soft-deleted ones you can change to this:

(new Model)->newQueryWithoutScopes()->forceDelete()

Solution 8 - Laravel

simple solution:

 Mymodel::query()->delete();

Solution 9 - Laravel

You can try this one-liner which preserves soft-deletes also:

Model::whereRaw('1=1')->delete();

Solution 10 - Laravel

The best way for accomplishing this operation in Laravel 3 seems to be the use of the Fluent interface to truncate the table as shown below

DB::query("TRUNCATE TABLE mytable");

Solution 11 - Laravel

The problem with truncate is that it implies an immediate commit, so if use it inside a transaction the risk is that you find the table empty. The best solution is to use delete

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

Solution 12 - Laravel

In a similar vein to Travis vignon's answer, I required data from the eloquent model, and if conditions were correct, I needed to either delete or update the model. I wound up getting the minimum and maximum I'd field returned by my query (in case another field was added to the table that would meet my selection criteria) along with the original selection criteria to update the fields via one raw SQL query (as opposed to one eloquent query per object in the collection).

I know the use of raw SQL violates laravels beautiful code philosophy, but itd be hard to stomach possibly hundreds of queries in place of one.

Solution 13 - Laravel

> In my case laravel 4.2 delete all rows ,but not truncate table

DB::table('your_table')->delete();

Solution 14 - Laravel

Solution who works with Lumen 5.5 with foreign keys constraints :

$categories = MusicCategory::all();
foreach($categories as $category)
{
$category->delete();
           
}
return response()->json(['error' => false]);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeteView Question on Stackoverflow
Solution 1 - LaravelbilalqView Answer on Stackoverflow
Solution 2 - LaravelYauheni PrakopchykView Answer on Stackoverflow
Solution 3 - LaravelFortexView Answer on Stackoverflow
Solution 4 - Laravelgiannis christofakisView Answer on Stackoverflow
Solution 5 - LaravelRejaulView Answer on Stackoverflow
Solution 6 - LaravellookitsatravisView Answer on Stackoverflow
Solution 7 - LaravelDave James MillerView Answer on Stackoverflow
Solution 8 - Laravelali filali View Answer on Stackoverflow
Solution 9 - LaraveljfeidView Answer on Stackoverflow
Solution 10 - LaravelPeteView Answer on Stackoverflow
Solution 11 - LaravelRiccardo VenturiniView Answer on Stackoverflow
Solution 12 - LaravelSidneyView Answer on Stackoverflow
Solution 13 - LaravelGanesan JView Answer on Stackoverflow
Solution 14 - LaravelAlain BerrierView Answer on Stackoverflow