Skip to content

fix(cloudflare-r2-binding): paginate list and batch delete past 1000 objects - #881

Open
debojyoti10CC wants to merge 1 commit into
unjs:mainfrom
debojyoti10CC:fix/r2-list-pagination
Open

debojyoti10CC wants to merge 1 commit into
unjs:mainfrom
debojyoti10CC:fix/r2-list-pagination

Conversation

@debojyoti10CC

@debojyoti10CC debojyoti10CC commented Nov 18, 2026 •

Copy link
Copy Markdown

Problem

getKeys and clear in the cloudflare-r2-binding driver call binding.list() once. R2 returns at most 1000 objects per call, so anything beyond the first 1000 was silently ignored: getKeys returned a truncated list and clear left the remaining objects in place.

Fix

getKeys now loops on the cursor while truncated is true. clear deletes the collected keys in chunks of 1000, since R2Bucket.delete accepts at most 1000 keys per call.

Testing

Added test/drivers/cloudflare-r2-binding-pagination.test.ts, which uses a fake bucket that pages 1000 at a time (2500 objects) and rejects deletes of more than 1000 keys. I put it in its own file because the existing R2 test file starts a wrangler platform proxy.

Before the fix:

× getKeys returns more than 1000 keys
  expected [ Array(1000) ] to have a length of 2500 but got 1000
× clear removes more than 1000 keys
  expected 1500 to be +0

After:

Test Files  1 passed (1)
     Tests  2 passed (2)

oxlint and oxfmt --check are clean for the changed files. I could not run the existing wrangler-based R2 test locally: it hung on my Windows machine.

Fixes #880

Summary by CodeRabbit

  • Bug Fixes

    • R2 key retrieval now includes matching objects across all result pages, including collections larger than 1,000 objects.
    • Clearing R2 objects now handles large collections by deleting them in batches of up to 1,000.
  • Tests

    • Added coverage for retrieving and clearing collections containing 2,500 objects.

…objects

getKeys now follows the list cursor while the result is truncated, and clear deletes in chunks of 1000 keys (the R2 per-call limit).
@debojyoti10CC
debojyoti10CC requested a review from pi0 as a code owner November 18, 2026 19:32
@vercel

vercel Bot commented Nov 18, 2026

Copy link
Copy Markdown

@debojyoti10CC is attempting to deploy a commit to the unjs Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The R2 binding now retrieves keys across list pages and deletes matching keys in batches of up to 1,000. Tests cover both operations with 2,500 keys.

Changes: R2 key operations

Layer / File(s) Summary
Cursor-based key listing
src/drivers/cloudflare-r2-binding.ts, test/drivers/cloudflare-r2-binding-pagination.test.ts
getKeys follows list cursors and accumulates keys from each page. A test checks retrieval of 2,500 keys.
Batched key deletion
src/drivers/cloudflare-r2-binding.ts, test/drivers/cloudflare-r2-binding-pagination.test.ts
clear deletes keys in batches of up to 1,000. A test checks batch sizes of 1,000, 1,000, and 500 for 2,500 keys.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 6e0b8

The R2 binding driver now lists and clears more than 1,000 objects by following list pages and deleting in batches. No actionable merge-blocking risk was found, and tests cover the 2,500-object case.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the cloudflare-r2-binding driver and the two changed behaviors: paginating list results and batch-deleting past 1000 objects. The changeset does both: getKeys follows list cursors, and…
Linked Issues check Passed Issue #880 requires two coding changes in the cloudflare-r2-binding driver. (1) getKeys must follow R2 truncated/cursor pagination. At head, src/drivers/cloudflare-r2-binding.ts implements this with a…
Out of Scope Changes check Passed The PR summary lists changes to only two files. These are src/drivers/cloudflare-r2-binding.ts and a new pagination test file. The driver changes are the two fixes required by #880: cursor pagination …
Full details: Title check

Explanation

The title names the cloudflare-r2-binding driver and the two changed behaviors: paginating list results and batch-deleting past 1000 objects. The changeset does both: getKeys follows list cursors, and clear deletes in batches of at most 1000. The title is specific and matches the main change, so it passes.

Full details: Linked Issues check

Explanation

Issue #880 requires two coding changes in the cloudflare-r2-binding driver. (1) getKeys must follow R2 truncated/cursor pagination. At head, src/drivers/cloudflare-r2-binding.ts implements this with a do/while loop. Each page passes cursor to binding.list, and the loop continues while page.truncated is true. (2) clear must delete all matching keys despite R2's 1000-key limit per delete. At head, clear collects keys through the internal getKeys, which returns full keys, then calls binding.delete in slices of 1000. Both behaviors match the issue. The PR also adds test/drivers/cloudflare-r2-binding-pagination.test.ts, which uses a fake bucket with 2500 objects and rejects deletes above 1000 keys. The PR reports that both tests pass. I did not read the test file contents. The diff tool was unavailable, so I did not compare against the base revision. The head source alone establishes the behavior described above. The existing wrangler-based R2 test was not run by the author, but that does not block the requirements.

Full details: Out of Scope Changes check

Explanation

The PR summary lists changes to only two files. These are src/drivers/cloudflare-r2-binding.ts and a new pagination test file. The driver changes are the two fixes required by #880: cursor pagination in getKeys and chunked deletes in clear. The new test covers those same behaviors. No unrelated refactoring, public API declaration changes, or other driver changes appear in the summary. The head source of the driver shows no unrelated logic. Public declarations are unchanged according to the summary.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: bf3212fc-fe84-4126-ab94-3e7b581676b8
Commits

Reviewing files that changed from the base of the PR and between 5781aae and 6e0b895.

Files selected for processing: 2

  • src/drivers/cloudflare-r2-binding.ts
  • test/drivers/cloudflare-r2-binding-pagination.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@debojyoti10CC

Copy link
Copy Markdown
Author

Bumping this in case it got buried. It is still ready for review, and I am happy to make any changes you would like. Thanks!

This branch has not been deployed

No deployments
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.

cloudflare-r2-binding: getKeys and clear ignore objects beyond the first 1000

1 participant