spatie/laravel-queued-db-cleanup
Safely delete large numbers of Laravel database records by dispatching non-overlapping queued cleanup jobs. Deletes in small chunks to reduce locks and avoid timeouts, automatically re-dispatching until the query’s records are gone.
CleanDatabaseJobFactory::new()) with optional chunk size tuning. Integrates seamlessly into existing Laravel job queues (e.g., database, redis).php artisan queue:work --sleep=3 --tries=3).php artisan queue:failed-table).Model::destroy() calls with queued jobs.ON DELETE CASCADE).CleanDatabaseJobFactory::new()
->query(YourModel::query()->where(...))
->beforeDeletion(fn () => event(new DeletionStarting))
->afterDeletion(fn () => event(new DeletionCompleted))
->dispatch();
Model::truncate() with queued jobs.CleanDatabaseJobFactory; no runtime tweaks needed.failed_jobs table. Use php artisan queue:failed-table to inspect.deleteChunkSize (e.g., 500–2000) based on DB load.php artisan tinker with bulk queries) before production.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Queue worker crashes | Partial deletions | Retry logic (Laravel’s --tries flag) |
| Database locks | Timeouts/blocked queries | Smaller chunks, monitor locks |
| Constraint violations | Job failures | Pre-validate or use ON DELETE CASCADE |
| Serverless timeouts | Incomplete deletions | Optimize chunk size, use larger timeouts |
| Disk space exhaustion | Worker failures | Monitor queue storage, archive logs |
php artisan queue:retry).How can I help you explore Laravel packages today?