Laravel Model Pruning in 2025: Auto‑Clean Your Database Before It Kills Your Server

Databases are like closets: if you don’t clean them regularly, they fill up with junk. Old logs, expired sessions, stale notifications, abandoned carts — all of it piles up until queries slow down, backups balloon, and your server starts gasping for air.

By 2025, Laravel has made this problem easier to solve with Model Pruning — a feature that automatically deletes outdated records from your database. Think of it as a scheduled cleaning crew that keeps your tables lean, fast, and healthy without manual intervention.


🚀 Why Model Pruning Matters

  • Performance: Large tables slow down queries, indexes, and joins.
  • Storage costs: Cloud databases charge by size; pruning saves money.
  • Maintainability: Cleaner datasets mean easier debugging and analytics.
  • Automation: No more writing custom cron jobs for every model.

🛠️ Step 1: Enable Prunable Models

Laravel provides a Prunable trait you can add to any Eloquent model.

Example: pruning old LoginAttempt records.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;

class LoginAttempt extends Model
{
    use Prunable;

    public function prunable()
    {
        // Delete records older than 30 days
        return static::where('created_at', '<=', now()->subDays(30));
    }
}

📊 Step 2: Run the Prune Command

Laravel ships with a built‑in Artisan command:

php artisan model:prune

This will scan all models using the Prunable trait and delete records matching their prunable() query.


🎨 Step 3: Automate with Scheduler

To keep pruning automatic, add it to your app/Console/Kernel.php:

protected function schedule(Schedule $schedule)
{
    $schedule->command('model:prune')->daily();
}

Now your database gets cleaned every day without you lifting a finger.


📈 Step 4: Advanced Example — Soft Deletes

Sometimes you don’t want to hard‑delete records immediately. You can combine pruning with soft deletes:

public function prunable()
{
    return static::where('deleted_at', '<=', now()->subMonths(6));
}

This way, records are soft‑deleted first, then permanently removed after six months.


🔧 Step 5: Multiple Models, Multiple Rules

You can prune different models with different rules:

  • Notifications: Delete after 90 days.
  • Sessions: Delete after 7 days.
  • Audit logs: Keep for 1 year, then prune.

Example:

class Notification extends Model
{
    use Prunable;

    public function prunable()
    {
        return static::where('created_at', '<=', now()->subDays(90));
    }
}

🧩 Comparing Old vs New Approaches

ApproachBefore 2025 (Manual)Now (Sanctum Pruning)
Cron jobsCustom scriptsBuilt‑in Artisan task
ConsistencyError‑proneCentralized & uniform
MaintenanceHigh effortLow effort
ScalabilityHard to manageEasy to extend

🌐 Real‑World Example: SaaS Cleanup

Imagine you’re running a SaaS product with millions of notifications and logs. Without pruning:

  • Queries slow down.
  • Backups take hours.
  • Storage bills skyrocket.

With pruning:

  • Old records vanish automatically.
  • Your database stays lean.
  • Your server breathes easy.

🔮 Looking Ahead

By 2025, Model Pruning has become a best practice in Laravel projects. It’s not just about deleting data — it’s about sustainable database hygiene. As apps scale, pruning ensures your infrastructure doesn’t collapse under the weight of stale records.


🧭 Final Thoughts

Laravel Model Pruning is your auto‑cleaner for databases. It’s:

  • Simple to set up.
  • Flexible for different models.
  • Automated with the scheduler.
  • Essential for performance and cost savings.

Don’t wait until your server chokes on millions of stale rows. Set up pruning today — and let Laravel keep your database clean, fast, and future‑proof.

Leave a Reply

Your email address will not be published. Required fields are marked *