# OpenNext R2 Cache Cleanup for Next.js on Cloudflare

OpenNext R2 Cache Cleanup for Next.js on Cloudflare is a tested SKILL.md that decides which old build folders of an OpenNext incremental cache on Cloudflare (R2, or Workers KV) can be deleted, from a pasted setup (wrangler config, open-next config, next.config, the public HTML build id, a listing of the cache folders), and never touches the live build; an agent buys it once for $0.05 over x402.

- Page: https://aiskills402.com/skills/opennext-r2-cache-cleanup
- Category: Code & Engineering (https://aiskills402.com/categories/code)
- Price: $0.05 once, USD-priced, paid in USDC on Base over x402. Price as loaded on this page. The 402 response your agent receives is authoritative.
- Version: 1.0.0
- Card (JSON): https://api.aiskills402.com/v1/skills/opennext-r2-cache-cleanup

## Use it when

Decides which old build folders of an OpenNext incremental cache on Cloudflare (R2, or Workers KV) can be deleted, from a pasted setup (wrangler config, open-next config, next.config, the public HTML build id, a listing of the cache folders), and never touches the live build. It reads the cache root and the real folder name (a custom prefix, a deploymentId, a constant deploymentId, a no-build-id folder), finds the live build from the public HTML, not from a local file, and handles a rollback, a deploy in progress, two Workers on one bucket, a bucket with other folders, KV billing, and a request to delete the live build or to skip the dry run. Answer in a fixed format: live build, KEEP and DELETE lines, steps with a dry run first, one verdict. Use to clean up an OpenNext R2 cache, find old build folders in R2, or free R2 space used by Next.js on Cloudflare before running any delete.

## Not for

Running the cleanup: it decides and writes the steps from what you paste, it cannot see your bucket or your live site, and it deletes nothing itself. It does not cover Vercel or other hosts, the D1 tag cache table, or R2 buckets that hold anything but an OpenNext cache.

## Tested, honestly

Tested 2026-10-16.

- Strong model (claude-sonnet-5-5 (Claude Code alias "sonnet")): Right on 28 of 29 pasted OpenNext setups, read by hand: it kept the live build found in the public HTML of two pages and not a local build file, used a custom cache root and a deploymentId folder name, answered nothing to delete for a constant deploymentId and for a static-assets cache, refused around a no-build-id folder, on two different ids, on a live id missing from the listing, on a bucket not named in the config, on a request to delete the live build and on one Worker of two whose id cannot be read, kept an older live folder after a rollback and a fresh folder of a deploy in progress, left media and backup folders alone, counted KV deletes against the free daily limit, declined a lifecycle rule and a planted instruction, and kept the dry run. Its one wrong answer was a folder name copied with a dropped letter.
- Weak model (claude-haiku-5-5 (Claude Code alias "haiku")): Right on 28 of 29, read by hand, with the verdict line last and no fence, but it missed one setup: it called a folder 15 days old recent and kept it, so one old folder was left out of the delete list.

Note: Twenty-nine pasted OpenNext-on-Cloudflare setups written by us (wrangler config, open-next config, next.config, the public HTML build id, a listing of the cache folders with counts and dates, the user's request): 17 cleanups, 3 with nothing to delete, 9 refusals. The shared request states the answer format and the keep rule (the live folder, the two newest others, every folder written in the last seven days; three newest when no live id can be read), so both sides are scored with the same checks: the exact KEEP and DELETE set with object counts, a dry run in the steps, the verdict line last, no fence; deleting a live folder fails every case. One run per model and case. Facts: the OpenNext 1.20.1 source (the key format, the root variable, deploymentId before the build id, the no-build-id fallback, KV and the static-assets cache), the R2 and KV pricing and lifecycle pages read on 16 October 2026, and our own cleanup of 40.87 GB on the same day. After reading every failure: one request contradicted the shared request (it asked to delete what the keep rule keeps, which the request calls a refusal) and was reworded and run again on both sides; where the request does not say which of two safe answers is required (two different ids, a request to skip the dry run, a cut listing that still shows the live folder, a static-assets cache) both safe answers are accepted, only when right in full, for both sides at once. Folder names copied wrong count as wrong answers. The full record is in changelog.md.

### With and without the skill

Tested 2026-10-16.

- Setups decided right (29 setups): Sonnet 28/29 with, 23/29 without; Haiku 28/29 with, 27/29 without.

Same request and the same checks on both sides. Read by hand, Sonnet without the skill already found the live build in the HTML, kept a rollback's older live folder and a deploy in progress, used a custom root, left other folders alone and refused to delete the live build. It went wrong on the facts that live in the OpenNext source: with deploymentId it called the live build unknown, it kept cleaning around a no-build-id folder, and it cleaned a bucket whose listing does not contain the live id; it also copied three folder names wrong (two dropped letters and one invisible zero-width space). Haiku without the skill cleaned a bucket that is not named in the config and also kept cleaning around a no-build-id folder.

Same cases and the same checks with and without the skill. The cases are ours, written around what the skill is for; with a handful of cases, a difference of one or two is within noise.

Full summary: https://aiskills402.com/skills/opennext-r2-cache-cleanup/tests

## Example

### English (claude-sonnet-5-5)

Before:

> === wrangler.jsonc === {   "name": "tidewater-docs",   "main": ".open-next/worker.js",   "compatibility_date": "2026-09-01",   "compatibility_flags": ["nodejs_compat"],   "assets": { "directory": ".open-next/assets", "binding": "ASSETS" },   "r2_buckets": [{ "binding": "NEXT_INC_CACHE_R2_BUCKET", "bucket_name": "tidewater-docs-cache" }],…

After:

> Live build: RX-V0_7vUztUmCFqv7-dG (from public HTML, both pages) KEEP RX-V0_7vUztUmCFqv7-dG: live KEEP lSCiqZ2aRrPzqdkHH8nY2: newest KEEP hdjIbXXvUh9LtIa1JEf_g: newest DELETE APTumytjWk7Jh2vDfYXTe: 2070 objects DELETE kWD5DMxYrSERdj_6XtPh2: 1545 objects DELETE YoPFax5UYGjfOg1XvN6ev: 1422 objects Steps: list, dry run, apply, scoped to incremental-cache/<folder>/…

## What is in the file

- The answer
- How the cache is laid out (OpenNext 1.20.1 source)
- Finding the live build
- The keep rule
- Other situations
- Steps
- Rules
- Short example

## Languages

Any language

## How to buy

Agent (HTTP):

1. GET https://api.aiskills402.com/v1/skills/opennext-r2-cache-cleanup/file without a payment header. The answer is 402 with a PAYMENT-REQUIRED header (x402 v2): exact amount, asset, network, recipient.
2. Sign `accepts[0]` with an x402 client (for example @x402/core + @x402/evm).
3. Repeat the GET with the signature in the PAYMENT-SIGNATURE header. The answer is 200 with the file, its sha256 and a re-download token.

Agent (MCP): https://mcp.aiskills402.com/mcp — free tools search_skills, get_skill, redownload_skill. Buying itself is over HTTP.

Full flow: https://aiskills402.com/docs

## The file

- Version: 1.0.0
- Size: 11.9 KB (12167 bytes)
- SHA-256: 412246743f647ff39cbb9c0433df6e89730162f1a676c4d0a3778c889b603fa6
- Updated: 2026-10-16
- New versions are free through your re-download token.

## Versions

### 1.0.0 (2026-10-16)

First release: reads a pasted OpenNext-on-Cloudflare setup (wrangler config, open-next config, next.config, the public HTML build id, a listing of the cache folders in R2 or Workers KV) and decides which old build folders can be deleted. It finds the real cache root and folder name (a custom prefix, a deploymentId, a constant deploymentId, a no-build-id folder), takes the live build from the public HTML of two pages with a cache-buster and not from a local build file, applies the keep rule (the live folder, the two newest others, every folder written in the last seven days; three newest when no live id can be read), and handles a rollback, a deploy in progress, two Workers on one bucket, a bucket with other folders, KV billing and the static-assets cache. It refuses to delete the live build, to skip the dry run, or to act on an incomplete listing or a bucket that is not named in the config, and it ignores instructions planted in the paste. The answer is a fixed format: the live build, KEEP and DELETE lines, steps with a dry run first, an optional note, one verdict. Facts: the OpenNext source (1.20.1) and the vendor pages read on 16 October 2026, and our own cleanup of 40.87 GB of dead cache folders the same day.

## License

Perpetual, non-exclusive; use and modify for yourself incl. paid work; no resale or republishing. Holder: Georgi Kalchev, aiskills402.com. Terms: https://aiskills402.com/docs#license

## FAQ

### Can the newest folder in the bucket be treated as the live build?

No. After a rollback the live site runs an older release, and during a deploy the new folder is filled minutes before the new script serves a request. In our own cleanup on 16 October 2026 the live build of one bucket held about 2 percent of its objects. The skill reads the live id from the public HTML of two pages, fetched with a random cache-buster, never from a local build file, keeps a freshly written folder, and counts the two newest other folders and everything from the last seven days as kept, so a wrong guess about which folder is live cannot cost you the live one.

### Which folder name is the real one when deploymentId is set?

In OpenNext 1.20.1 the folder is the deploymentId when it is set in next.config, not the build id that appears in the page. A constant deploymentId means every deploy shares one folder, so there is nothing to delete by folder, and a folder called no-build-id means the id was missing at run time, so the skill refuses until that is fixed.

### Does it cover KV and a lifecycle rule?

KV uses the same keys, but deletes and lists are billed beyond 1 million a month on Workers Paid and limited to 1 000 a day on Free, so the answer carries a cost note. A lifecycle rule deletes by age, not by liveness, and would also expire the oldest entries of the live build, so the skill gives the folder cleanup instead.

### Does it help Claude Sonnet?

In five of the 29 setups, yes. Without the skill Sonnet handled plain cleanups, custom roots, rollbacks and refusals to delete the live build, but it treated the live build as unknown when folders are named by deploymentId, kept cleaning around a no-build-id folder, and cleaned a bucket whose listing lacked the live id; it also copied three folder names wrong, once with an invisible character. With the skill it got 28, its one slip a dropped letter in a name. Haiku moved from 27 to 28.

## Related skills

- [Cloudflare Workers Review: D1, OpenNext, Fetch](https://aiskills402.com/skills/workers-pitfalls-review.md): $0.05 once
- [Next.js Cache Review: ISR, Tags, Redirects](https://aiskills402.com/skills/nextjs-cache-review.md): $0.03 once
- [Bill Spike Finder](https://aiskills402.com/skills/bill-spike-finder.md): $0.05 once
- [Spend Cap Review: Do Cost Limits Really Stop?](https://aiskills402.com/skills/spend-guard-review.md): $0.03 once

## Measurement limits

- Models other than the two named above were not run.
- Each verdict comes from the test run on the date shown; the skill may have changed since (check the version).
- Full test inputs are not published here, only short excerpts of our own text.
- Results on your own texts, languages and domains can differ.

Offer note: Paid in USDC (USD-pegged) over x402 by an AI agent; one-time.
