Skip to main content

Prunable and MassPrunable — Cleaning Up Old Records

Premium
3 min read

Production applications accumulate records over time: expired tokens, old log entries, soft-deleted rows that were never fully removed. Laravel's Prunable and MassPrunable traits let you define exactly which records should be cleaned up and automate that process with a scheduler.


Prunable

Add the Prunable trait to your model and define a prunable() method returning the query that selects records to delete.

app/Models/PasswordResetToken.php:

use Illuminate\Database\Eloquent\Prunable;
 
class PasswordResetToken extends Model
{
use Prunable;
 
public function prunable(): Builder
{
return static::where('created_at', '<', now()->subDays(7));
}
}

Running the artisan command will delete all...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (34 h 11 min)

You also get:

58 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…

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.