Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Queued Db Cleanup Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Use Case Alignment: Ideal for Laravel applications requiring batch deletion of large datasets (e.g., log cleanup, soft-deleted records, or compliance-driven purging). Fits well in event-driven or scheduled workflows (e.g., cron jobs, Laravel tasks).
  • Database Abstraction: Works with Eloquent models, leveraging Laravel’s query builder. Compatible with MySQL, PostgreSQL, SQLite, and likely others (tested in CI).
  • Non-Blocking Design: Avoids table locks by chunking deletions, critical for high-concurrency systems (e.g., SaaS platforms with shared databases).
  • Serverless Readiness: Addresses execution timeouts (e.g., AWS Lambda) via queued processing, aligning with modern serverless architectures.

Integration Feasibility

  • Minimal Boilerplate: Requires only a single method call (CleanDatabaseJobFactory::new()) with optional chunk size tuning. Integrates seamlessly into existing Laravel job queues (e.g., database, redis).
  • Dependency Lightweight: Single package with no external services (except Laravel’s queue system). No database migrations or schema changes required.
  • Event Hooks: Supports observers or listeners for pre/post-deletion logic (e.g., analytics, notifications) via Laravel’s event system.

Technical Risk

  • Queue Backpressure: Heavy deletion workloads may overwhelm the queue system if not monitored. Requires queue worker scaling (e.g., Supervisor, Kubernetes HPA).
  • Transaction Safety: Chunked deletions are not atomic by default. Risk of partial deletions if jobs fail mid-execution (mitigated by retries, but business logic may need idempotency checks).
  • Foreign Key Constraints: Deletions may violate constraints if related records exist. Requires manual handling (e.g., cascading deletes or pre-validation).
  • Performance Variability: Deletion speed depends on database engine, indexing, and chunk size. Benchmarking recommended for production workloads.

Key Questions

  1. Queue Infrastructure: Does the team have a scalable queue system (e.g., Redis, RabbitMQ) to handle peak loads?
  2. Monitoring: How will deletion progress/job failures be tracked? (Suggest: Laravel Horizon or custom metrics.)
  3. Idempotency: Are there business rules requiring retries to be safe (e.g., deduplication)?
  4. Database Locking: Are there long-running transactions or high-write workloads that could conflict with deletions?
  5. Testing: How will edge cases (e.g., empty tables, constraint violations) be validated in CI/CD?

Integration Approach

Stack Fit

  • Laravel Ecosystem: Native support for Laravel 8+ (tested up to v10.x). Compatible with:
    • Queues: Database, Redis, SQS, etc.
    • Jobs: Extends Laravel’s job system; no custom queue drivers needed.
    • Events: Integrates with Laravel’s event system for pre/post hooks.
  • Database Compatibility: Works with Eloquent models and raw queries. Tested on MySQL/PostgreSQL; SQLite may need chunk size adjustments.
  • Serverless: Optimized for AWS Lambda, Cloud Functions, or similar (avoids timeout issues).

Migration Path

  1. Pilot Phase:
    • Start with non-critical tables (e.g., logs, cache).
    • Validate chunk size (default: 1000) via load testing.
  2. Queue Setup:
    • Configure workers (e.g., php artisan queue:work --sleep=3 --tries=3).
    • Monitor queue backlog (e.g., php artisan queue:failed-table).
  3. Gradual Rollout:
    • Replace direct Model::destroy() calls with queued jobs.
    • Use feature flags for critical paths.
  4. Fallback Plan:
    • Maintain manual deletion scripts as a backup.

Compatibility

  • Laravel Versions: Tested on 8.x–10.x. May require minor adjustments for older versions.
  • Database Drivers: No active driver dependencies, but transactions may behave differently per DB (e.g., PostgreSQL’s ON DELETE CASCADE).
  • Custom Logic: Extend via:
    CleanDatabaseJobFactory::new()
        ->query(YourModel::query()->where(...))
        ->beforeDeletion(fn () => event(new DeletionStarting))
        ->afterDeletion(fn () => event(new DeletionCompleted))
        ->dispatch();
    

Sequencing

  1. Pre-requisites:
    • Ensure queue workers are running.
    • Set up monitoring (e.g., Laravel Horizon, Prometheus).
  2. Implementation Order:
    • Step 1: Replace simple Model::truncate() with queued jobs.
    • Step 2: Add pre/post hooks for business logic.
    • Step 3: Optimize chunk size based on performance metrics.
  3. Post-Launch:
    • Schedule regular cleanup jobs (e.g., via Laravel Tasks or cron).
    • Document failure handling (e.g., retry logic, alerts).

Operational Impact

Maintenance

  • Package Updates: Low effort—MIT license, minimal breaking changes (monitor Spatie’s changelog).
  • Configuration: Centralized via CleanDatabaseJobFactory; no runtime tweaks needed.
  • Logging: Requires custom instrumentation (e.g., log deleted record counts, durations).

Support

  • Debugging: Failed jobs appear in Laravel’s failed_jobs table. Use php artisan queue:failed-table to inspect.
  • Performance Tuning: Adjust deleteChunkSize (e.g., 500–2000) based on DB load.
  • Documentation: Lightweight; relies on Spatie’s README and Laravel’s queue docs.

Scaling

  • Horizontal Scaling: Add more queue workers to parallelize deletions.
  • Vertical Scaling: Optimize chunk size for DB performance (e.g., smaller chunks for slow queries).
  • Load Testing: Simulate peak loads (e.g., php artisan tinker with bulk queries) before production.

Failure Modes

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

Ramp-Up

  • Onboarding: 1–2 hours for basic integration; additional time for custom hooks/monitoring.
  • Training: Focus on:
    • Queue worker management.
    • Chunk size tuning.
    • Failure recovery (e.g., php artisan queue:retry).
  • Documentation Gap: Add internal runbooks for:
    • Emergency rollback (e.g., restore from backup if deletions are catastrophic).
    • Performance baselines (e.g., "10K records take ~5 mins with chunk=1000").
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport