Skip to main content
Quick Tip

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.

class File extends Model
{
use SoftDeletes;
 
// Add Prunable trait
use Prunable;
 
public function prunable()
{
// Files matching this query will be pruned
return static::query()->where('deleted_at', '<=', now()->subDays(14));
}
 
protected function pruning()
{
// Remove the file from s3 before deleting the model
Storage::disk('s3')->delete($this->filename);
}
}
 
// Add PruneCommand to your schedule (app/Console/Kernel.php)
$schedule->command(PruneCommand::class)->daily();

Tip by @Philo01

Enjoyed This Tip?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Recent Courses

Laravel Modules and DDD

16 lessons
1 h 59 min

How to Build Laravel 12 API From Scratch

28 lessons
1 h 21 min

Laravel 12 For Beginners: Your First Project

15 lessons
1 h 32 min

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.