Vivek Mistry 👋

I’m a Certified Senior Laravel Developer with 6+ years of experience , specializing in building robust APIs and admin panels, frontend templates converting them into fully functional web applications.

Book A Call
  • 12 Sep, 2025
  • 172 Views
  • Hidden feature called Prunable Models

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!)

Share: