research/wrangler-pages-cache-pins-account
Why Wrangler Keeps Deploying to the Wrong Cloudflare Account: The pages.json Cache
Engineering Note · November 9, 2026 · 6 minute read · Cloudflare · Wrangler · Deployment
Wrangler, Cloudflare’s command-line tool, remembers the account and project from your last Pages command in a small file called pages.json, and it reuses them when you do not say otherwise. If a deploy goes to the wrong account or project, that file is the first thing to check. On Wrangler 4.158.0 we found that the CLOUDFLARE_ACCOUNT_ID environment variable overrides the cached account, that --project-name overrides the cached project, and that deleting the file clears both.
Everything below was run with Node.js 22.23.4 and Wrangler 4.158.0 on Linux. We sent no real deployments. We used a fake API token, turned telemetry off with WRANGLER_SEND_METRICS=false, and read the API address each command tried to call from Wrangler’s debug log (WRANGLER_LOG=debug). The requests failed at the network, which is fine here, because the address shows which account and project Wrangler picked.
Where is the file?
Reading Wrangler’s bundled source (wrangler-dist/cli.js), the cache folder is chosen in this order:
node_modules/.cache/wranglerunder the nearestnode_modulesfolder, if that folder already exists..wrangler/cachein the current directory, if that exists.- Otherwise
node_modules/.cache/wranglerif anode_modulesfolder was found, and.wrangler/cacheif not.
The file name is pages.json. The code saves two fields to it: account_id after any Pages command that authenticates (we saw it in pages project list, pages project create and pages deploy), and project_name after a successful pages deploy. We confirmed by reading the code that these are the saved fields. We did not run a real deploy to see the file written.
In a project that ignores node_modules, the file is already ignored. In ours, git check-ignore -v reported the path matched the node_modules/ rule.
What did we test?
We made a scratch project with a dist/index.html, wrote a cache file by hand, and ran Pages commands with a fake token.
mkdir -p node_modules/.cache/wrangler
cat > node_modules/.cache/wrangler/pages.json <<'JSON'
{
"account_id": "11111111111111111111111111111111",
"project_name": "old-project"
}
JSON
export CLOUDFLARE_API_TOKEN=fake-token-for-test
export WRANGLER_LOG=debug WRANGLER_SEND_METRICS=false
unset CLOUDFLARE_ACCOUNT_ID
For each command we kept only the API path from the debug output, with a grep -o.
Test 1: the cache alone
npx wrangler pages project list
The path Wrangler tried:
accounts/11111111111111111111111111111111/pages/projects
With nothing else set, it used the cached account.
Test 2: the environment variable against the cache
CLOUDFLARE_ACCOUNT_ID=22222222222222222222222222222222 npx wrangler pages project list
accounts/22222222222222222222222222222222/pages/projects
The variable won. The cache file was ignored for the account.
Test 3: the cached project name
npx wrangler pages deploy dist
accounts/11111111111111111111111111111111/pages/projects/old-project
We gave no project name, and Wrangler reused old-project from the file, with the cached account. The source shows the project name is taken from --project-name, then from the config file’s name, then from the cache, and the cached name is used only when the cached account equals the account Wrangler is authenticated as.
Test 4: an explicit project name
npx wrangler pages deploy dist --project-name new-one
accounts/11111111111111111111111111111111/pages/projects/new-one
The flag beat the cached project name. The account still came from the cache, because we had not set the variable. That is the combination that bites in practice: you fix the project and the account stays wrong.
Test 5: deleting the file
rm node_modules/.cache/wrangler/pages.json
npx wrangler pages project list
With no file and no variable, the pages project list command no longer printed an account path. Wrangler had no cached account to use, and our fake token could not supply one. With a real token in an interactive terminal, Wrangler would go on to ask for or look up an account. We did not test that path.
What surprised us?
Our first run of Test 1 did not behave. The command called an account we had not written in the file. The sandbox we ran in already had CLOUDFLARE_ACCOUNT_ID set in its environment, and the variable beat the cache, as Test 2 shows. We had not looked at the environment first. Once we unset it for the tests, the results above followed.
The practical lesson is that there are three places an account can come from, and Wrangler does not print which one it used unless you ask. Check them in this order:
- The environment. Run
env | grep -i cloudflare. A variable set in a shell profile, a CI secret or a.envloader counts. - The cache file. Run
cat node_modules/.cache/wrangler/pages.json, and also look for.wrangler/cache/pages.json. - The command line.
--project-namefor the project.
We did not test whether an account_id in a Wrangler config file changes the order, so we leave that out.
How do you fix it?
Pick the smallest change that matches the situation.
| Situation | Fix | What we tested |
|---|---|---|
| One-off deploy to a specific account | Set CLOUDFLARE_ACCOUNT_ID on that command | Beats the cache (Test 2) |
| Wrong project name | Pass --project-name | Beats the cache (Test 4) |
| Cache is stale after you switch accounts | Delete pages.json | Cleared the cached account (Test 5) |
| CI that must never use a cache | Set the variable explicitly in the job | Same behavior as Test 2 |
For scripts, make the account explicit instead of relying on whatever the last command left behind. A deploy script that sets both values and fails if either is missing cannot go to the wrong place by accident:
: "${CLOUDFLARE_ACCOUNT_ID:?set CLOUDFLARE_ACCOUNT_ID}"
npx wrangler pages deploy dist --project-name "${PAGES_PROJECT:?set PAGES_PROJECT}"
We ran the first line in bash with the variable unset. It printed CLOUDFLARE_ACCOUNT_ID: set CLOUDFLARE_ACCOUNT_ID and the shell exited with status 127 before reaching anything after it. We did not run the deploy line against Cloudflare.
Does logging out clear it?
The source suggests so. A function named purgeConfigCaches is registered to run on login and logout (purgeOnLoginOrLogout). We read this in the code and did not run wrangler login or wrangler logout, so treat it as a lead, not a result. If you switch accounts by logging in again and still see the old one, check the environment variable first, then the file.
Limitations
- We tested one version, Wrangler 4.158.0, on Linux with Node.js 22.23.4. Cache behavior may differ in other versions.
- We never completed a real deployment. We read the API path from the debug log and the cache’s contents from the source. The saved fields after a successful deploy come from reading the code.
- We tested Pages commands only. We did not test Workers deploys,
wrangler loginorwrangler logout, or anaccount_idin a config file. - The fake token meant every request failed. Real authentication could change what Wrangler does when no account is found.
- The account IDs in the examples are made up. The one that surprised us was a real value in the sandbox, which we have not printed.
Related reading
- Structuring an Astro Static Site with Cloudflare Pages Functions
- D1 Migrations for a Small Product: Additive Changes and a Local-First Workflow
- A DST-Safe Weekday Cron in GitHub Actions: Two Schedules and a Guard
Frequently asked questions
Where does Wrangler store the account and project it remembers?
In a file called pages.json inside a cache folder. Wrangler uses node_modules/.cache/wrangler when a node_modules folder exists above you, and falls back to .wrangler/cache in the current directory. The file holds an account_id and, after a deploy, a project_name.
Which wins: the cache file or CLOUDFLARE_ACCOUNT_ID?
The environment variable. On Wrangler 4.158.0 we set a cache file pointing at one account and the variable pointing at another. The command called the account from the variable. The cache only applies when the variable is not set.
How do I make Wrangler forget the account?
Delete pages.json from the cache folder. After we did, a Pages command with no variable set no longer had a cached account to use. Setting CLOUDFLARE_ACCOUNT_ID, or passing --project-name for the project, overrides what the file says.
Is the cache file safe to commit?
It does not belong in version control. It is machine-specific state, and it lives under node_modules, which most projects already ignore. In our project, git reported that the path is ignored by the node_modules/ rule.
Does this affect Workers deployments or only Pages?
We only tested Pages commands. The cache file is named pages.json and the code that reads it is in the Pages commands, so we would not assume it affects Workers deploys, and we did not test that.