0
0
LaravelHow-ToBeginner · 3 min read

How to Use Delete in Eloquent: Laravel Model Deletion Guide

In Laravel Eloquent, you can delete a record by calling the delete() method on a model instance or by using destroy() with one or more IDs. The delete() method removes the record from the database permanently.
📐

Syntax

The delete() method is called on an Eloquent model instance to remove that record from the database. Alternatively, destroy() is a static method that accepts one or multiple primary key IDs to delete records directly.

  • Instance delete: Deletes the specific model instance.
  • Static destroy: Deletes records by their IDs without needing to retrieve them first.
php
use App\Models\User;

// Delete a single model instance
$user = User::find(1);
$user->delete();

// Delete by IDs directly
User::destroy(2);
User::destroy([3, 4, 5]);
💻

Example

This example shows how to find a user by ID and delete it using delete(). It also demonstrates deleting multiple users by IDs using destroy().

php
<?php
use App\Models\User;

// Find user with ID 10
$user = User::find(10);

if ($user) {
    $user->delete(); // Deletes user with ID 10
    echo "User 10 deleted successfully.";
} else {
    echo "User not found.";
}

// Delete users with IDs 11 and 12 directly
User::destroy([11, 12]);
echo "Users 11 and 12 deleted.";
Output
User 10 deleted successfully.Users 11 and 12 deleted.
⚠️

Common Pitfalls

Common mistakes when deleting with Eloquent include:

  • Calling delete() on a query builder instead of a model instance, which will cause an error.
  • Not checking if the model exists before calling delete(), leading to errors.
  • Using destroy() with invalid or non-existing IDs silently does nothing, so always verify.
php
<?php
// Calling delete() on query builder
User::where('id', 1)->delete(); // This works but returns number of deleted rows, not a model method

// Correct: delete on model instance
$user = User::find(1);
if ($user) {
    $user->delete();
}

// Wrong: calling delete() without checking
$user = User::find(9999); // might be null
// $user->delete(); // Error: call on null (commented out to avoid error)

// Correct: check before delete
if ($user) {
    $user->delete();
}
📊

Quick Reference

MethodUsageDescription
delete()Called on model instanceDeletes the specific record from database
destroy()Called statically with ID(s)Deletes record(s) by primary key(s) without retrieving
forceDelete()Called on model instancePermanently deletes record ignoring soft deletes
Soft DeletesUse SoftDeletes traitMarks record as deleted without removing from DB

Key Takeaways

Use delete() on a model instance to remove that record from the database.
Use destroy() statically with one or more IDs to delete records directly.
Always check if the model exists before calling delete() to avoid errors.
Calling delete() on a query builder deletes records but does not trigger model events.
For soft deletes, use the SoftDeletes trait and forceDelete() to permanently remove.