210

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!

Rishabh
  • 3,752
  • 4
  • 47
  • 74
Pete
  • 7,289
  • 10
  • 39
  • 63

14 Answers14

404

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.

Basil Musa
  • 8,198
  • 6
  • 64
  • 63
bilalq
  • 7,279
  • 3
  • 22
  • 28
  • Cool, didn't know about that one! Thanks! Just out of curiosity (and for future readers) is there a way to do a similar thing in Laravel 3? Or is there simply no supported way of deleting all rows in a table in Laravel 3 (other than resorting to PDO or something)? – Pete Mar 19 '13 at 01:55
  • 18
    Note: truncate() also resets any AUTO_INCREMENT counter (also note you can't truncate tables which have foreign key constraints.) – William Turrell Jul 24 '16 at 13:06
  • 12
    FYI: Turncate will not trigger delete events. – Fusion Aug 01 '16 at 19:25
  • 1
    If you really want to use `MyModel::all()->delete()`, use `foreach (MyModel::all() as $e) { $e->delete() }` – Ema4rl Jan 02 '17 at 11:13
  • Even after truncating the child table, Eloquent was still complaining that it cannot truncate the parent table because of foreign key reference. @Yauheni's answer below worked for me. – dotNET Aug 20 '18 at 06:59
  • truncate can delete the whole table and re-create it which is a lot faster compared to deleting each and every row. restting AUTO_INCREMENT counter is a sideeffect of a newly created table. – chilly Sep 11 '18 at 18:47
  • `truncate()` may fail when you have setup foreign keys – Adam Dec 09 '19 at 15:36
121

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();
Ketav
  • 760
  • 1
  • 8
  • 27
Yauheni Prakopchyk
  • 10,202
  • 4
  • 33
  • 37
  • 11
    In case anyone else was confused about why this works, the Model class forwards methods to the Builder via the __call magic method [here](https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/Model.php#L3478-L3494). Because the model class itself has a delete method, calling Model::delete() calls the Model method, when you really want the Builder method. So to get the builder explicitly, you can use getQuery(). – kevinAlbs Mar 07 '16 at 18:32
  • 1
    This also doesnt delete related tables if you want that. – Terje Nesthus Sep 22 '17 at 04:24
  • It will force delete all records ,irrespective whether soft delete is on or off – shalini Mar 07 '18 at 06:02
  • 2
    Model::whereNotNull('id')->delete(); -- will do soft delete when soft delete is ON – shalini Mar 07 '18 at 06:05
76

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");
hasan.hameed
  • 124
  • 9
Fortex
  • 761
  • 5
  • 2
52

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.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
15

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.

lookitsatravis
  • 712
  • 5
  • 15
  • `Model::delete();` will accomplish the same thing. – Leng Sep 03 '14 at 01:43
  • 6
    Unfortunately `Model::delete()` throws an exception `Non-static method Illuminate\Database\Eloquent\Model::delete() should not be called statically`, at least in Laravel 5.0. – Dave James Miller Feb 25 '16 at 10:44
13

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

Oscar Gallardo
  • 2,240
  • 3
  • 27
  • 47
Rejaul
  • 831
  • 1
  • 12
  • 35
  • 1
    @aschipfl not much to explain actually. The code run the SQL `DELETE FROM users WHERE id LIKE '%%'` which matches all the rows in the table, thus deleting everything. – Hkan Nov 23 '15 at 14:01
  • This got me on my way. I ended up doing a pluck() on another model to get an array of the ID's I needed, then used that array to delete all the records from my model using the `whereIn` method: `$itemsAllContentIDs = Item::where('user_id', $userId)->pluck('item_content_id')->all();` `ItemsContent::whereIn('id', $itemsAllContentIDs)->delete();` – Keith DC Nov 03 '16 at 07:24
13

simple solution:

 Mymodel::query()->delete();
ali filali
  • 310
  • 4
  • 8
9

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()
Dave James Miller
  • 4,928
  • 3
  • 20
  • 18
7

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

Model::whereRaw('1=1')->delete();
jfeid
  • 71
  • 1
  • 1
5

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

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");
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
Pete
  • 7,289
  • 10
  • 39
  • 63
0

MyModel::truncate();

in commade line :

php artisan tinker

then

Post::truncate();

-1

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.

Sidney
  • 624
  • 7
  • 20
-1

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

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

Ganesan J
  • 539
  • 7
  • 12