{"record":{"id":"3393227ab03340b7","repo":"router-for-me/CLIProxyAPI","slug":"failed-to-remove-file-w","errorCode":null,"errorMessage":"failed to remove file: %w","messagePattern":"failed to remove file: %w","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"internal/api/handlers/management/auth_files_crud.go","lineNumber":368,"sourceCode":"\tif targetAuth := h.findAuthForDelete(name); targetAuth != nil {\n\t\tif !isPluginVirtualSourceDelete(name, targetAuth) {\n\t\t\treturn filepath.Base(name), http.StatusConflict, errPluginVirtualAuth\n\t\t}\n\t\ttargetID = strings.TrimSpace(targetAuth.ID)\n\t\tif path := strings.TrimSpace(authAttribute(targetAuth, \"path\")); path != \"\" {\n\t\t\ttargetPath = path\n\t\t}\n\t}\n\tif !filepath.IsAbs(targetPath) {\n\t\tif abs, errAbs := filepath.Abs(targetPath); errAbs == nil {\n\t\t\ttargetPath = abs\n\t\t}\n\t}\n\tif errRemove := os.Remove(targetPath); errRemove != nil {\n\t\tif os.IsNotExist(errRemove) {\n\t\t\treturn filepath.Base(name), http.StatusNotFound, errAuthFileNotFound\n\t\t}\n\t\treturn filepath.Base(name), http.StatusInternalServerError, fmt.Errorf(\"failed to remove file: %w\", errRemove)\n\t}\n\tif errDeleteRecord := h.deleteTokenRecord(ctx, targetPath); errDeleteRecord != nil {\n\t\treturn filepath.Base(name), http.StatusInternalServerError, errDeleteRecord\n\t}\n\th.removeAuthsForPath(ctx, targetPath, targetID)\n\treturn filepath.Base(name), http.StatusOK, nil\n}\n\nfunc isPluginVirtualSourceDelete(name string, auth *coreauth.Auth) bool {\n\tif !coreauth.IsPluginVirtualAuth(auth) {\n\t\treturn true\n\t}\n\tsourcePath := strings.TrimSpace(authAttribute(auth, coreauth.AttributeVirtualSource))\n\tif sourcePath == \"\" {\n\t\tsourcePath = strings.TrimSpace(authAttribute(auth, \"path\"))\n\t}\n\tif sourcePath == \"\" {\n\t\treturn false","sourceCodeStart":350,"sourceCodeEnd":386,"githubUrl":"https://github.com/router-for-me/CLIProxyAPI/blob/78f0c4079e3e6273d65d03b5549cffc898703264/internal/api/handlers/management/auth_files_crud.go#L350-L386","documentation":"deleteAuthFileByName removes the target file from disk with os.Remove after resolving it under authDir (or the auth record's stored `path`). If Remove fails and it is not a not-exist case (which maps to 404 errAuthFileNotFound), the error wraps as `failed to remove file: %w` with HTTP 500. Causes are environmental: permission denied on file/dir, read-only filesystem, or the file being a non-empty directory.","triggerScenarios":"authDir file owned by root while the proxy runs unprivileged; auths/ mounted read-only in a container; file has immutable attribute; targetPath resolved from the auth record's `path` attribute points somewhere the process cannot write; attempting to remove a directory with entries.","commonSituations":"Docker auths volumes owned by root; SELinux/AppArmor policies; files created by a previous run under a different user; NFS mounts with root_squash.","solutions":["Read the wrapped OS error: EACCES -> fix ownership/permissions of the file and authDir; EROFS -> remount volume writable.","In containers, mount auths/ writable and owned by the process user.","Verify the auth record's `path` attribute points to a location the proxy may delete.","Retry after fixing permissions — the record cleanup (deleteTokenRecord, removeAuthsForPath) runs only after successful removal."],"exampleFix":"# before: root-owned auth file, proxy runs as app user\n$ ls -l auths/x.json  # -rw------- root root\n\n# after\n$ chown $(id -u app):$(id -g app) auths/x.json && chmod u+rw auths/x.json","handlingStrategy":"validation","validationCode":"func canDeleteAuthFile(path string) bool {\n    info, err := os.Stat(path)\n    if err != nil { return false }\n    // writable check: try opening for write\n    f, err := os.OpenFile(path, os.O_WRONLY, 0o600)\n    if err != nil { return false }\n    _ = f.Close()\n    return true\n}","typeGuard":"func isRemoveFailure(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"failed to remove file\")\n}","tryCatchPattern":null,"preventionTips":["Ensure the process user owns/writes both the auth files and authDir.","Mount auths/ writable in containers.","Verify auth record `path` attributes point to deletable locations."],"tags":["management-api","filesystem","permissions","delete"],"backgroundTag":null,"analyzedSha":"78f0c4079e3e6273d65d03b5549cffc898703264","analyzedAt":"2026-08-15T12:26:37.444Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}