Model Pruning to Auto-Clean Old Records
Laravel Tip: Use “Model Pruning” to Auto-Clean Old Records
It lets you automatically delete stale or unnecessary rows from your database — no need for custom cleanup jobs.
How It Works:
Add Prunable to a model:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
class ActivityLog extends Model
{
use Prunable;
public function prunable()
{
// Delete logs older than 30 days
return static::where('created_at', '<=', now()->subDays(30));
}
}
Run the Command
php artisan model:prune
Automate with Scheduler
use Illuminate\Support\Facades\Schedule;
Schedule::command('model:prune')->daily();
Why It’s Special
- Keeps DB lean & fast without manual cleanup
- Works on any model (logs, notifications, tokens, drafts)
- Almost nobody uses it (hidden gem!)