Add cleanup-r2-orphans command for bulk R2 deletion#109
Merged
Conversation
Follow-up to #108. The serial loop added there is fine for the handful of deactivations per 5-min pipeline cycle, but the one-time historical backfill (~67k packages × 2 keys = ~135k DeleteObject calls) projects to ~5 hours at the observed prod rate of ~3.7 packages/sec. Add wppackages cleanup-r2-orphans, a one-off command that uses the S3 DeleteObjects batch API (up to 1000 keys per request) parallelized across cfg.R2.Concurrency workers. Tracks per-key failures from DeleteObjectsOutput.Errors and clears deployed_hash per-batch with a final pass for packages whose keys spanned multiple batches. Safe to interrupt and re-run. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #108. The band-aid added there uses a serial loop in
SyncToR2()to delete R2 files for deactivated packages — fine for the handful of deactivations per 5-min pipeline cycle, but far too slow for the one-time backfill needed after #108 ships.The backfill needs to clear ~67k packages × 2 keys = ~135k DeleteObject calls. At ~135ms per call, the serial loop projects to ~5 hours and blocks the pipeline (oneshot service skips timer ticks while running). Confirmed empirically on prod: ~3.7 packages/sec.
This PR adds
wppackages cleanup-r2-orphans, a one-off CLI command that uses the S3DeleteObjectsbatch API (up to 1000 keys per request) parallelized acrosscfg.R2.Concurrencyworkers. ~135 batches at ~50-wide concurrency should finish in seconds, not hours.What it does
internal/deploy/cleanup.go— newBulkDeleteDeactivated():is_active = 0 AND deployed_hash IS NOT NULL(same predicate asSyncToR2)(package_id, key)pairsDeleteObjectsacross anerrgroupcapped atcfg.R2.ConcurrencyDeleteObjectsOutput.Errorsfielddeployed_hashper-batch (best effort) and again in a final pass for packages whose keys spanned multiple batchesdeployed_hashcleared, so a subsequent run only picks up what's leftcmd/wppackages/cmd/cleanup_r2_orphans.go— thin Cobra wrapper that callsBulkDeleteDeactivated()and logs the result.How to run (one-time, on prod)
After this, the regular per-deploy serial loop in
SyncToR2is sufficient — only a few packages get deactivated per cycle.🤖 Generated with Claude Code