{"record":{"id":"7374acef34d58d1c","repo":"cloudflare/cloudflared","slug":"s-already-exists","errorCode":null,"errorMessage":"%s already exists","messagePattern":"(.+?) already exists","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cloudflared/tunnel/subcommands.go","lineNumber":304,"sourceCode":"\twarningChecker := updater.StartWarningCheck(c)\n\tdefer warningChecker.LogWarningIfAny(sc.log)\n\n\t_, err = sc.create(name, c.String(CredFileFlag), c.String(createSecretFlag.Name))\n\treturn errors.Wrap(err, \"failed to create tunnel\")\n}\n\nfunc tunnelFilePath(tunnelID uuid.UUID, directory string) (string, error) {\n\tfileName := fmt.Sprintf(\"%v.json\", tunnelID)\n\tfilePath := filepath.Clean(fmt.Sprintf(\"%s/%s\", directory, fileName))\n\treturn homedir.Expand(filePath)\n}\n\n// writeTunnelCredentials saves `credentials` as a JSON into `filePath`, only if\n// the file does not exist already\nfunc writeTunnelCredentials(filePath string, credentials *connection.Credentials) error {\n\tif _, err := os.Stat(filePath); !os.IsNotExist(err) {\n\t\tif err == nil {\n\t\t\treturn fmt.Errorf(\"%s already exists\", filePath)\n\t\t}\n\t\treturn err\n\t}\n\tbody, err := json.Marshal(credentials)\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Unable to marshal tunnel credentials to JSON\")\n\t}\n\treturn os.WriteFile(filePath, body, 0400)\n}\n\nfunc buildListCommand() *cli.Command {\n\treturn &cli.Command{\n\t\tName:        \"list\",\n\t\tAction:      cliutil.ConfiguredAction(listCommand),\n\t\tUsage:       \"List existing tunnels\",\n\t\tUsageText:   \"cloudflared tunnel [tunnel command options] list [subcommand options]\",\n\t\tDescription: \"cloudflared tunnel list will display all active tunnels, their created time and associated connections. Use -d flag to include deleted tunnels. See the list of options to filter the list\",\n\t\tFlags: []cli.Flag{","sourceCodeStart":286,"sourceCodeEnd":322,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/cmd/cloudflared/tunnel/subcommands.go#L286-L322","documentation":"writeTunnelCredentials saves the tunnel's JSON credentials file, but only when the destination does not already exist. If os.Stat finds an existing file at filePath (and stat itself succeeded), cloudflared refuses to overwrite it and raises this error, protecting existing credentials from being clobbered by a new tunnel's credentials.","triggerScenarios":"`cloudflared tunnel create <name>` (or `token` command) writing credentials to a path where a file already exists — typically re-creating a tunnel whose credentials file survived a previous create, or using a credentials-file path that collides with another tunnel's file.","commonSituations":"Re-running `tunnel create` after a partial run; reusing a config's credentials-file path for a new tunnel; stale JSON left after the tunnel was deleted remotely but the local file kept.","solutions":["Delete or move the existing file at the credentials path, then re-run the create/token command.","If the existing file belongs to a still-valid tunnel, use it directly instead of creating a new tunnel.","Point the command's --credentials-file (or output path) at a fresh, unique location."],"exampleFix":"// before\ncloudflared tunnel create my-tunnel   # ~/.cloudflared/<id>.json already exists\n// after\nrm ~/.cloudflared/<old-id>.json   # or back it up\ncloudflared tunnel create my-tunnel","handlingStrategy":"validation","validationCode":"credPath := \"/home/user/.cloudflared/<id>.json\"\nif _, err := os.Stat(credPath); err == nil {\n    return fmt.Errorf(\"credentials file %s exists; remove or reuse it\", credPath)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remove or archive old credentials JSON before re-running `tunnel create`.","Use unique credentials-file paths per tunnel.","Treat an existing credentials file as a signal the tunnel may already exist — check `tunnel list` first."],"tags":["cli","file","conflict","credentials"],"backgroundTag":"file-already-exists","analyzedSha":"2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f","analyzedAt":"2026-09-06T04:14:33.757Z","contentChangedAt":"2026-09-06T04:14:33.757Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}