Laravel Soft Delete By Example – Quick Start Guide

laravel soft delete

When working on a web application, have you ever wished you could delete something but still keep it around just in case? Laravel’s soft delete feature lets you do just that. It’s a clever way to mark data as “deleted” without actually removing it from your database. This feature is especially handy when you need a safety net for accidental deletions or want to keep an audit trail.

What Is Soft Delete?

Normally, when you delete a record in a database, it’s gone forever. That’s called a hard delete. But what if you want to “hide” the record instead of wiping it out entirely? That’s where soft delete comes in. Laravel uses a special deleted_at column to track when a record was soft deleted. If this column is empty, the record is active. If it has a timestamp, the record is considered deleted—but it’s still in your database.

Why Use Soft Deletes?

So why bother with soft deletes? Here are some key reasons:

  • Mistake Recovery: Accidentally deleted a record? No problem—just restore it!
  • Audit and Compliance: Keep a trail of all changes for auditing purposes.
  • Improved User Experience: Enable features like undo delete or archived views for users.

Soft deletes offer a practical way to protect your data and make your app more robust.

How to Set Up Soft Deletes in Laravel

1. Add the deleted_at Column

First, you need to update your database table. Add the deleted_at column using Laravel’s softDeletes method in your migration:

Schema::table('users', function (Blueprint $table) {
    $table->softDeletes();
});

This creates the deleted_at column, which Laravel uses to track soft deletes.

See also  Deploy a Laravel Application on AWS EKS: A Step-By-Step Guide

2. Use the SoftDeletes Trait in Your Model

Next, tell Laravel that your model should support soft deletes. Add the SoftDeletes trait to your model:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;
}

That’s it—your model is now soft delete-ready.

Performing Soft Delete Operations

Deleting a Record

To soft delete a record, just call the delete() method:

$user = User::find(1);
$user->delete();

This sets the deleted_at column to the current timestamp, marking the record as deleted.

Querying Deleted Records

Want to see deleted records? Laravel makes it easy:

  • Include All Records (Deleted and Active): $users = User::withTrashed()->get();
  • Only Deleted Records: $deletedUsers = User::onlyTrashed()->get();

Restoring Soft Deleted Records

If you need to bring a soft-deleted record back to life, use the restore() method:

$user = User::withTrashed()->find(1);
$user->restore();

This removes the timestamp from deleted_at, making the record active again.

Permanently Deleting Records

Sometimes, you need to permanently delete records. Laravel provides the forceDelete() method for this:

$user = User::withTrashed()->find(1);
$user->forceDelete();

This completely removes the record from your database.

Best Practices for Using Soft Deletes

  • Clean Up Regularly: Use forceDelete to purge old, unnecessary data.
  • Maintain Relationships: Ensure related models also support soft deletes to avoid orphaned records.
  • Optimize Queries: Be mindful of performance when querying large datasets with soft deletes.

Laravel’s soft delete functionality is a lifesaver when you need more control over your data. By keeping deleted records intact, you can recover from mistakes, comply with audit requirements, and offer better user experiences. Try it out in your next Laravel project—you’ll love the flexibility it provides!

Photo of author
As Editor in Chief of HeatWare.net, Sood draws on over 20 years in Software Engineering to offer helpful tutorials and tips for MySQL, PostgreSQL, PHP, and everyday OS issues. Backed by hands-on work and real code examples, Sood breaks down Windows, macOS, and Linux so both beginners and power-users can learn valuable insights.

Leave a Comment