Delete All Records from Table in Laravel Eloquent
Delete All Records from Table in Laravel Eloquent

Delete All Records from Table in Laravel Eloquent

Laravel, one of the most popular PHP frameworks, makes database interactions seamless with Eloquent ORM. One common task developers encounter is deleting all records from a table. This guide will walk you through the steps to achieve this using Eloquent in Laravel, ensuring the process is straightforward and easy to understand.

Prerequisites

Before we dive into the code, make sure you have the following set up:

  1. Laravel Application: Ensure you have a Laravel project set up. If not, you can create one using the command:
   composer create-project --prefer-dist laravel/laravel blog
  1. Database Configuration: Configure your database settings in the .env file located at the root of your Laravel project.

Deleting All Records Using Eloquent

Eloquent ORM provides a simple and intuitive syntax for interacting with the database. Deleting all records from a table can be done in a few different ways.

Method 1: Using truncate()

The truncate method is the most efficient way to delete all records from a table. It resets the auto-incrementing ID to zero.

use App\Models\User;

public function deleteAllUsers()
{
    User::truncate();
}

Explanation:

  • User is the Eloquent model representing the users table.
  • truncate() method deletes all records and resets the auto-incrementing ID.

Method 2: Using delete()

The delete method removes records but does not reset the auto-incrementing ID.

use App\Models\User;

public function deleteAllUsers()
{
    User::query()->delete();
}

Explanation:

  • User::query()->delete() fetches all records and deletes them.
  • This method might be slower for large datasets compared to truncate.

Practice Code Example

Let’s see a complete example where we create a command to delete all users from the users table.

  1. Create a Custom Command

First, generate a custom Artisan command:

php artisan make:command DeleteAllUsers
  1. Implement the Command Logic

Open the generated command file located at app/Console/Commands/DeleteAllUsers.php and implement the logic:

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\User;

class DeleteAllUsers extends Command
{
    protected $signature = 'users:delete-all';
    protected $description = 'Delete all users from the users table';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        User::truncate();
        $this->info('All users have been deleted.');
    }
}
  1. Register the Command

Register your command in the Kernel class located at app/Console/Kernel.php:

protected $commands = [
    \App\Console\Commands\DeleteAllUsers::class,
];
  1. Run the Command

Execute the command in the terminal:

php artisan users:delete-all

This command will delete all records from the users table and output a confirmation message.

Summary

Deleting all records from a table in Laravel Eloquent is simple and can be achieved using either the truncate or delete methods. The truncate method is faster and resets the auto-incrementing ID, while the delete method just removes the records. For practical implementation, creating a custom Artisan command can be very handy.

By following this guide, you should be able to delete all records from any table in your Laravel application with ease. Happy coding!