Skip to content

Conversation

adityaanurag0219
Copy link

Summary

When running the php artisan model:prune command on very large datasets, MySQL can throw this error:

SQLSTATE[HY000]: General error: 1206 The total number of locks exceeds the lock table size

This happens because deleting many records in a single transaction can overwhelm InnoDB’s lock table.

This PR updates the pruneAll() method to wrap each chunk (default 1000 rows) within individual transactions. That way, locks are released between chunks, reducing memory and lock pressure.


Changes

  • Inside src/Illuminate/Database/Eloquent/Prunable.php, wrap each chunk’s iteration in:
    DB::beginTransaction();
    // delete chunk...
    DB::commit();
  • Preserve the existing chunkById() behavior, event dispatch (ModelsPruned), and exception handling.

Why This Change Matters

  • Mitigates lock table overflow errors in high-volume prune runs
  • Keeps behavior consistent in smaller datasets
  • Respects existing Laravel conventions (chunking, events, soft deletes)
  • Fully backward compatible — calling code & API not changed

Testing & Validation

  • All existing test suites run without failure (we only altered prune logic)
  • Code formatting ensured via Laravel Pint, matching style rules
  • No new dependencies introduced

Additional Notes

  • This is a targeted database-level fix; it doesn’t alter the public API or introduce breaking changes
  • Style-only fixes (if any) will be auto-merged by maintainers per the Laravel Contribution Guide
  • If there is feedback (e.g. regarding chunk size defaults), I’m happy to adjust

@adityaanurag0219
Copy link
Author

Thanks @taylorotwell — appreciate you reviewing this.
I understand it might target a later release.
Happy to adjust this for 13.x or include tests if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants