ItSolutionStuff.com

Delete All Records from Table in Laravel Eloquent

By Hardik Savani • April 16, 2024
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, laravel 10 and laravel 11.

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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent inRandomOrder() Method Example

Read Now →

Laravel Eloquent whereNotBetween() Query Example

Read Now →

Laravel Eloquent selectRaw() Query Example

Read Now →

Laravel Eloquent whereRaw Condition Example

Read Now →

How to Group By with Order By Desc in Laravel?

Read Now →

Laravel Order By Relation Column Example

Read Now →

Laravel Eloquent WhereNotIn Query Example

Read Now →

How to Create and Use Query Scope in Laravel Eloquent

Read Now →

How to use Union Query with Laravel Eloquent?

Read Now →

Laravel Has Many Through Eloquent Relationship Tutorial

Read Now →

Laravel Where Clause with date_format() Example

Read Now →