• Reading time ~ 2 min
  • 20.01.2023

The Laravel Deleted Models package by Spatie automatically copies deleted models to a separate table:

— Freek Van der Herten 🔭 (@freekmurze) January 16, 2023

According to Freek's writeup about the package, you can think of this package as a "recycle bin for models. The package achieves this through a database table called deleted_models and adding a KeepsDeletedModels trait to your models that you want to save backups of deleted models:

use Illuminate\Database\Eloquent\Model;
use Spatie\DeletedModels\Models\Concerns\KeepsDeletedModels;

class BlogPost extends Model
    use KeepsDeletedModels;

This package can also attempt to restore deleted models using a few different methods:

// $blogPost will be restored and returned
$blogPost = BlogPost::restore(5);

// $blogPost will be returned, but it is not saved in the DB yet
$blogPost = Blogpost::makeRestored($id);


You can also tap into the restore process with a Closure if you want to modify the model during the restoration process. This package has other configuration options and considerations, like pruning the deleted models table on a schedule and how often that table is pruned.

You might wonder why you would use this package vs. the built-in soft deletes feature in Laravel core. Freek Van der Herten's writeup does an excellent job of comparing the tradeoffs of both approaches.

In summary, here are some pros and cons of soft deletes:

  • ➕ Soft deletes is very convenient
  • ➕ No data copying needed when "deleting" a model
  • ➕ Deleted records benefit from any future alterations to the table schema
  • ➕ You must keep soft deletes in mind when querying the database (i.e., scope where deleted_at=null)
  • ➖ Referential integrity when soft deleting model associations, this must be done manually
  • ➖ Undelete can be complex when restoring a model (and related models)

And according to Freek, here are some pros and cons of using a separate deleted models table:

  • ➕ No need to adjust queries to account for deleted_at=null
  • ➕ The database can protect referential integrity—deleting a record will also delete all associated records
  • ➕ Easy to permanently delete old data
  • ➖ Harder to restore deleted data as you must copy it back (vs. setting the deleted_at=null to restore a model with simple soft deletes)

You can learn more about this package, get full installation instructions, and view the source code on GitHub.


No comments yet
Yurij Finiv

Yurij Finiv

Full stack


Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
Ukraine, Lutsk