Delete All Records from Table in Laravel Eloquent

By Hardik Savani November 5, 2023 Category : Laravel

Now, let's see article of laravel delete all rows from table. We will use laravel delete all records from table. you can understand a concept of laravel delete records from table. we will help you to give example of how to delete all records from table in laravel.

Let's get started with delete all records from table in laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10.

Do you require to delete all data from table using laravel eloquent? If yes then you can get a solution for your problem. We can easily remove all records from table using DB class with delete().

But if you need to destroy all records using laravel eloquent model then how you will do it?, Actually i cache my all records and when someone remove that records then automatic remove from cache too. but it is possible if you are doing with laravel eloquent model. So is there method truncate() with eloquent but it is not working for me, so finally i found solution for delete all records from database table using following way.

Let's see bellow examples:

Example 1:

<?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:

<?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::whereNotNull('id')->delete();

}

}

Example 3:

<?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 4:

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

}

}

I hope it can help you...

Shares