laravel
  1. laravel-delete-all-records-from-table-in-laraveleloquent

Delete All Records from Table in Laravel Eloquent - Laravel Misc.

In this article, we’ll look at how to delete all records from a table using Laravel’s Eloquent ORM.

Syntax

\Model::truncate();

Example

use App\Models\User;

User::truncate();

Output

null

Explanation

The truncate() method deletes all the records from a table and resets the auto-incrementing ID to zero. The truncate() method is faster than the delete() method because it doesn't fire any delete event or trigger any on delete referential actions, such as comparing foreign keys.

Use

We can use truncate() method when we want to delete all records from a table without having to loop through and delete each record individually. This feature is especially useful when we're testing or want to delete all data from a table at once.

Important Points

  • The truncate() method deletes all data from a table.
  • It is faster than the delete() method.
  • The truncate() method resets the auto-incrementing ID to zero.

Summary

Using the truncate() method, we can delete all records from a table and reset the auto-incrementing ID to zero in a single operation. This method is faster than the delete() method and can be useful in a variety of scenarios, such as testing or deleting all data from a table at once.

Published on: