{"record":{"id":"9a33e237d05c1f65","repo":"cloudflare/cloudflared","slug":"the-file-writing-error-is-v-the-delete-tunnel","errorCode":null,"errorMessage":"The file-writing error is: %v / The delete tunnel error is: %v / The tunnel was deleted, because the tunnel can't be run without the credentials file (joined error lines for failed credentials-file write)","messagePattern":"The file-writing error is: (.+?) / The delete tunnel error is: (.+?) / The tunnel was deleted, because the tunnel can't be run without the credentials file \\(joined error lines for failed credentials-file write\\)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cloudflared/tunnel/subcommand_context.go","lineNumber":186,"sourceCode":"\t\tcredentialsFilePath, err = tunnelFilePath(tunnelCredentials.TunnelID, originCertDir)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tusedCertPath = true\n\t}\n\twriteFileErr := writeTunnelCredentials(credentialsFilePath, &tunnelCredentials)\n\tif writeFileErr != nil {\n\t\tvar errorLines []string\n\t\terrorLines = append(errorLines, fmt.Sprintf(\"Your tunnel '%v' was created with ID %v. However, cloudflared couldn't write tunnel credentials to %s.\", tunnel.Name, tunnel.ID, credentialsFilePath))\n\t\terrorLines = append(errorLines, fmt.Sprintf(\"The file-writing error is: %v\", writeFileErr))\n\t\tif deleteErr := client.DeleteTunnel(tunnel.ID, true); deleteErr != nil {\n\t\t\terrorLines = append(errorLines, fmt.Sprintf(\"Cloudflared tried to delete the tunnel for you, but encountered an error. You should use `cloudflared tunnel delete %v` to delete the tunnel yourself, because the tunnel can't be run without the tunnelfile.\", tunnel.ID))\n\t\t\terrorLines = append(errorLines, fmt.Sprintf(\"The delete tunnel error is: %v\", deleteErr))\n\t\t} else {\n\t\t\terrorLines = append(errorLines, \"The tunnel was deleted, because the tunnel can't be run without the credentials file\")\n\t\t}\n\t\terrorMsg := strings.Join(errorLines, \"\\n\")\n\t\treturn nil, errors.New(errorMsg)\n\t}\n\n\tif outputFormat := sc.c.String(outputFormatFlag.Name); outputFormat != \"\" {\n\t\treturn nil, renderOutput(outputFormat, &tunnel)\n\t}\n\n\tfmt.Printf(\"Tunnel credentials written to %v.\", credentialsFilePath)\n\tif usedCertPath {\n\t\tfmt.Print(\" cloudflared chose this file based on where your origin certificate was found.\")\n\t}\n\tfmt.Println(\" Keep this file secret. To revoke these credentials, delete the tunnel.\")\n\tfmt.Printf(\"\\nCreated tunnel %s with id %s\\n\", tunnel.Name, tunnel.ID)\n\n\treturn &tunnel.Tunnel, nil\n}\n\nfunc (sc *subcommandContext) list(filter *cfapi.TunnelFilter) ([]*cfapi.Tunnel, error) {\n\tclient, err := sc.client()","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/cmd/cloudflared/tunnel/subcommand_context.go#L168-L204","documentation":"During Named Tunnel creation, after the tunnel is created remotely, cloudflared writes the credentials file. If BOTH the credentials-file write and the subsequent tunnel-delete cleanup fail, it joins the two error messages plus an explanatory line into a single error, warning that an orphaned tunnel exists that cannot run without its credentials file.","triggerScenarios":"The directory for the credentials file (default ~/.cloudflared) is not writable or missing, AND the follow-up `cloudflared tunnel delete` also fails (e.g. API/auth error).","commonSituations":"Read-only home directory; running as wrong user; disk full; expired or missing API token so the cleanup delete is rejected.","solutions":["Fix write permissions on the credentials directory: `mkdir -p ~/.cloudflared && chmod u+w ~/.cloudflared`.","Check disk space and quota; free space if full.","Manually delete the orphaned tunnel: `cloudflared tunnel delete <tunnel-ID>`.","Inspect the embedded %v messages in the error for the underlying write and delete causes and address each."],"exampleFix":"// before\n$ cloudflared tunnel create mytunnel\n// after\n$ mkdir -p ~/.cloudflared && chmod u+w ~/.cloudflared\n$ cloudflared tunnel create mytunnel","handlingStrategy":"try-catch","validationCode":"if [ ! -w \"${HOME}/.cloudflared\" ] && ! mkdir -p \"${HOME}/.cloudflared\"; then\n  echo \"cannot write credentials directory\" >&2; exit 1\nfi","typeGuard":null,"tryCatchPattern":"_, err := createTunnel(ctx)\nif err != nil && strings.Contains(err.Error(), \"can't be run without the credentials file\") {\n    // read embedded write/delete causes, fix permissions, then delete orphaned tunnel\n}","preventionTips":["Ensure ~/.cloudflared (or --cred-file dir) exists and is writable before `tunnel create`.","Run as a user with a writable home directory.","On failure, clean up orphaned tunnels with `cloudflared tunnel delete <ID>`."],"tags":["filesystem","permissions","tunnel","cleanup"],"backgroundTag":"file-write-failed","analyzedSha":"2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f","analyzedAt":"2026-09-06T04:14:33.757Z","contentChangedAt":"2026-09-06T04:14:33.757Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}