“I created an AI assistant for Laravel developers that handles all the boring work.”
Learn more
Smousss
Benjamin Crozat The art of crafting web applications

Laravel soft delete: the definitive guide

Benjamin Crozat — Updated on

Table of contents

Laravel soft delete: the definitive guide

What is a soft delete?

A soft delete is the action of marking a model as deleted without really deleting it from the database.

Imagine a deleted_at column in your database containing the date where your entry has been deleted.

How will you benefit from soft deletes

The main benefit of soft deletes is that you don’t loose data anymore. You will always be able to restore it.

You could also set up a scheduled task to clean up old soft deleted models given enough time passed. (Once they’re definitely deleted, you will be able to rely on your backups.)

How to set up a soft delete

Laravel requires you to take two easy steps to set up a soft delete.

First, specify that you need a column for soft deletion in your migration (I wrote a nice article about migrations). Once you run it, you’ll see a new deleted_at column in your posts table.

public function up()
{
Schema::table('posts', function (Blueprint $table) {
+ $table->softDeletes();
});
}

In the down() method, you can remove the column using the dropSoftDeletes() method.

public function down()
{
Schema::table('posts', function (Blueprint $table) {
+ $table->dropSoftDeletes();
});
}

Then, in your model, import the SoftDeletes trait.

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
 
class Post extends Model
{
+ use HasFactory, SoftDeletes;
}

How to perform a soft delete

To soft delete in Laravel, you don’t have to change your habits. Use your model’s delete() method just like before. The only difference will be that Laravel will add the current date and time inside the deleted_at column.

$post->delete();

How to check if a model has been soft deleted

To check if a model has been soft deleted, use the trashed() method.

if ($post->trashed()) {
//
}

More helpers for soft deletes

Sometimes, you may need to include soft deleted models in your queries. The withTrashed() scope can help with that.

Post::withTrashed()->get();

You can even query trashed models only:

Post::onlyTrashed()->get();

Also, since the model is never really deleted, you can restore it at any moment using the restore() method. The deleted_at column will be back to NULL.

$post->restore();

Finally, if you want to really remove a soft deletable model from the database definitely, use the forceDelete() method.

$post->forceDelete();

How to test for a soft delete

To test for a soft delete in Laravel, use the assertSoftDeleted() method Laravel provides.

Here’s a basic example of how I’d go for it:

public function test_it_soft_deletes_posts()
{
$post = Post::factory()->create();
 
$this
->deleteJson(route('posts.destroy', $post))
->assertNoContent();
 
$this->assertSoftDeleted($post);
}

The inverse method exists as assertNotSoftDeleted().

How to clean up old soft deleted models

You can use the pruning mechanism Laravel offers to clean up old soft deleted models.

For example, import the Prunable trait inside your model and tell the framework to remove models that have been soft deleted since a month or more.

namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
 
class Post extends Model
{
use HasFactory, Prunable, SoftDeletes;
 
public function prunable()
{
return static::where('deleted_at', '<=', now()->subMonth());
}
}

Don’t forget to run the model:prune command with the scheduler. Add it to your app/Console/Kernel.php:

protected function schedule(Schedule $schedule)
{
+ $schedule->command('model:prune')->daily();
}
Recommended

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Migrations are essential in any Laravel app using a database. I will tell you what they are, why you should use them and how you can generate them.

Laravel 10 has been released on February 14, 2023. Let's dive into every relevant new feature and change.

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

Laravel Collections make arrays more powerful and convenient to work with. This article provides tons of quick tips to instantly make your codebase better.

Dive into the history of Laravel. If you went away for some time, this is the right place to resume your journey.

Start leveraging the power of AI today. It enables developers to do incredible things, and many startups build products around it.

I show you how to upgrade your Laravel 9 project to version 10 and help you decide whether the return on investment is worth it.

Store and manage files on Dropbox and use it to back up your Laravel app automatically. Compatible with PHP 8.1+ and Laravel 9+.

I show you how to upgrade your Laravel 8 project to version 9 and help you decide whether the return on investment is worth it.

Powered by