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? (36 h 00 min)

You also get:

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

Already a member? Login here

No comments yet…