How To Delete All Records From Table In Laravel

How To Delete All Records From Table In Laravel

In this tutorial, we will explain to you different methods to delete all records from the table in Laravel. You can easily remove the record with the following examples.

Sometimes we need to delete all the records from the table at once. Let’s see a scenario where we need data on a daily basis of users in that we need users all activity. And at the end of the day, that data should be deleted from the main table and should be stored in others table as an archive. In that scenario, we need to delete all the data from that particular table. So in this post, we will learn how to delete all the records at once in Laravel.

In this post, we are using Laravel eloquent model and Laravel query builder to delete all the records from the database table.

So basically there are 3 options that you can use to delete the records.

The first one is using the truncate(). truncate delete all the table data from the database and set the index position at 1. So if you want to reset and delete everything in the table then you should use the truncate.

Second is using delete() and like. In like when you put %% it matches all the id and removes them from the database. Unliked truncate() it does not reset the index position at 1. So basically if you have a primary column in the table which has set to auto-increment, then that auto-increment index value does not get reset using delete().

Let”s suppose your next column auto-increment value is 101 then if you delete all the records from the table using delete(), then the increment value does not reset to the initial position at 1. But in truncate it gets reset to 1. This is basically the main difference in the truncate and delete.

In third we are using query builder, in which we select the table and delete all the records using delete()

Let’s see the example to delete all rows from the table.

Example 1:

Laravel delete all records from the table using truncate

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        User::truncate();
    }
}

Example 2:

Laravel delete all records from the table using delete

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        User::where('id', 'like' '%%')->delete();
    }
}

Example 3:

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use DB;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        DB::table('users')->delete();
    }
}

Conclusion:

So we have explained all possible scenario to delete all the record from the database table in the Laravel. We have tried to exmplain all the concept using the example, hope it helped you.

If You have any queries related to this post, please comment below i would love to help you.

You can also check How to use Session in Laravel