Soft Delete & Force Delete Example in Laravel

Last modified on February 7, 2020 1 min read

In this tutorial, we’re going to learn how soft delete works in Laravel. When models are soft deleted, they are not actually removed from your database. Instead, a timestamp is set on the deleted_at column.

Table of Contents

  1. Create Migration & Model
  2. Insert New Data
  3. Soft Delete
  4. Restore Deleted Data
  5. Force Delete (Permanently)

Create Migration & Model

We’ll test soft delete for products table. Let’s create a migration and model for products table:

php artisan make:model Product -m

Go to database/migrations folder and open timestamp_create_products_table.php file. We need to define $table->softDeletes(); line like this:


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('products', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

From the app folder, open Product.php file. In the Product model, we have to include use SoftDeletes; line.


namespace App;

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

class Product extends Model
    use SoftDeletes;

    public $fillable = ['title', 'price'];

Insert New Data

For testing purposes, I’ll test insert, delete tasks using Laravel Tinker. Let’s open Laravel Tinker by this command:

php artisan tinker

I’m inserting a new product:

$product = new Product;
$product->title = 'Test Product';
$product->price = 4.32;

We’ve inserted a new product. We can see all inserted products:

$products = Product::get();

Soft Delete

Normal delete will be the soft delete. Let’s delete the product softly:

// or,

If we soft delete any product, the delete_at column will be updated. To see the deleted data, we can write code like:

$products = Product::onlyTrashed()->get();

If we need to get both deleted and fresh data, there is a way to get both:

$products = Product::withTrashed()->get();

Restore Deleted Data

We can easily restore any deleted data like this:


Force Delete (Permanently)

If we want to delete permanently, we have this option:


To delete from soft-deleted (trashed) data, we need to write code like:


We can also set conditions at the time of deleting data from the trash. Let’s deleted 30 days of older data from soft deleted data.

Product::onlyTrashed()->where('deleted_at', '<', Carbon::subDays(30))->forceDelete();
The tutorial is over. Thank you. 🙂

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.


2 Replies to “Soft Delete & Force Delete Example in Laravel”

Leave a Reply