Home / Snippets / How to Delete All Rows or Truncate Model in Laravel
How to Delete All Rows or Truncate Model in Laravel cover

How to Delete All Rows or Truncate Model in Laravel

2.5K

3 years ago

0 comments

To delete all of your rows, you can make use of the "truncate()" method available on your model.
YourModel::truncate();

Laravel Truncate Code Example
So for example, if you want to delete all of your "Article" you can define it like below. Do note that the code below is defined within the "routes/web.php" file
Route::get('/delete-articles', function () {
  Article::truncate();
});

So now when you visit "/delete-article", all of the articles will be deleted and emptied.

Delete All
One other way to delete all of the model records, you can make use of the "Article::query()->delete()". The advantage of using this method is that you get to listen to the Eloquent Event.
Article::query()->delete();
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this