🚀 Available for Freelance, Remote, and Full-Time Opportunities. Let's build something amazing together.

Laravel Packages
1 min read

Laravel Chores: Resumable Data Operations and Cleanups

Laravel Chores is a package for executing idempotent, resumable background data cleanup operations with state persistence.

A

Author

1 week ago

Idempotent Data Operations with Laravel Chores

Running multi-hour database cleanups or data backfills in production can be prone to network timeouts or queue worker restarts. Laravel Chores provides a resilient structure for defining resumable background operations.

namespace App\Chores;

use Laravel\Chores\Chore;

class PruneOldAuditLogs extends Chore
{
    public int $chunkSize = 5000;

    public function handle(): void
    {
        AuditLog::where('created_at', '<', now()->subYears(2))
            ->chunkById($this->chunkSize, function ($logs) {
                foreach ($logs as $log) {
                    $log->delete();
                    $this->advance();
                }
            });
    }
}