Prunable trait to automatically remove models from your database

New in Laravel 8.50: You can use the Prunable trait to automatically remove models from your database. For example, you can permanently remove soft deleted models after a few days.

1class File extends Model
2{
3 use SoftDeletes;
4 
5 // Add Prunable trait
6 use Prunable;
7 
8 public function prunable()
9 {
10 // Files matching this query will be pruned
11 return static::query()->where('deleted_at', '<=', now()->subDays(14));
12 }
13 
14 protected function pruning()
15 {
16 // Remove the file from s3 before deleting the model
17 Storage::disk('s3')->delete($this->filename);
18 }
19}
20 
21// Add PruneCommand to your schedule (app/Console/Kernel.php)
22$schedule->command(PruneCommand::class)->daily();

Tip by @Philo01

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord