Skip to content

wrangler d1 migrations apply --remote --temporary fails with 'already authenticated' even with no prior login #15171

Description

@vladimirbakalov

What version of Wrangler are you using?

4.122.0

What operating system are you using?

macOS

Describe the Bug

wrangler d1 migrations apply --remote --temporary fails with:

X [ERROR] You're already authenticated with Cloudflare, so `--temporary` can't be used. Temporary preview accounts are only for unauthenticated use. Either remove `--temporary` to use your existing account, or log out (and unset CLOUDFLARE_API_TOKEN) first.

...even when the only prior interaction with --temporary in the session was wrangler d1 create <name> --temporary and wrangler kv namespace create <name> --temporary, run against a freshly-created temp preview account (no wrangler login was ever run, and CLOUDFLARE_API_TOKEN/CLOUDFLARE_ACCOUNT_ID were not set in the environment).

The auth check for d1 migrations apply appears to treat the cached temp-account credential file at ~/Library/Preferences/.wrangler/wrangler-temporary-account.toml (written by earlier --temporary commands in the same session) as evidence of a real login, and refuses to proceed — even though --temporary is explicitly passed on this exact invocation and no real wrangler login ever happened.

Steps to reproduce

  1. In a clean environment with no prior wrangler login and no CLOUDFLARE_API_TOKEN/CLOUDFLARE_ACCOUNT_ID set:
    wrangler deploy --temporary
    
    (accept the resulting temp preview account claim)
  2. Create a D1 database against the same temp account:
    wrangler d1 create my-db --temporary
    
  3. Apply migrations to that database, still passing --temporary:
    wrangler d1 migrations apply my-db --remote --temporary
    

Expected

Migrations apply against the temp preview account's D1 instance, same as the create/deploy commands did.

Actual

Fails immediately with You're already authenticated with Cloudflare, so --temporary can't be used, even though no real login exists — only cached temp-account state from steps 1-2 in the same session.

Workaround found

Extract account.id and apiToken from the cached ~/Library/Preferences/.wrangler/wrangler-temporary-account.toml, export them as CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN, then run the migrations command without --temporary:

export CLOUDFLARE_ACCOUNT_ID=<account.id from the toml file>
export CLOUDFLARE_API_TOKEN=<apiToken from the toml file>
wrangler d1 migrations apply my-db --remote

This applies cleanly against the real remote D1 instance tied to the temp account. It works, but it means --temporary is functionally unsupported for the d1 migrations apply --remote subcommand specifically, unlike d1 create, kv namespace create, and deploy, which all correctly accept --temporary in the same session.

Relevant code

The check appears to live around wrangler-dist/cli.js's "already authenticated" telemetry message (user temporary account already authenticated), which doesn't seem to special-case d1 migrations apply's own --temporary flag the way sibling commands do.

Metadata

Metadata

Assignees

No one assigned

    Labels

    product:d1Relating to Cloudflare D1: https://developers.cloudflare.com/d1/

    Type

    No type

    Projects

    Status
    Untriaged

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions