{"record":{"id":"9d27e94c4d944038","repo":"cloudflare/cloudflared","slug":"error-removing-s-w","errorCode":null,"errorMessage":"error removing %s: %w","messagePattern":"error removing (.+?): %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cloudflared/linux_service.go","lineNumber":548,"sourceCode":"\t\t}\n\t}\n\treturn nil\n}\n\nfunc uninstallOpenRC(log *zerolog.Logger) error {\n\tif err := runCommand(\"rc-service\", cloudflaredOpenRCService, \"stop\"); err != nil {\n\t\tlog.Warn().Err(err).Msg(\"could not stop cloudflared OpenRC service, continuing uninstall\")\n\t}\n\tif err := runCommand(\"rc-update\", \"del\", cloudflaredOpenRCService, \"default\"); err != nil {\n\t\tlog.Warn().Err(err).Msg(\"could not remove cloudflared from the default runlevel, continuing uninstall\")\n\t}\n\tfor _, template := range []ServiceTemplate{openrcTemplate, openrcConfTemplate} {\n\t\tpath, err := template.ResolvePath()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error resolving OpenRC template path: %w\", err)\n\t\t}\n\t\tif err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {\n\t\t\treturn fmt.Errorf(\"error removing %s: %w\", path, err)\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc copyFile(src, dest string) error {\n\tsrcFile, err := os.Open(src) //nolint:gosec // operator-provided service config path\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer func() { _ = srcFile.Close() }()\n\n\tdestFile, err := os.Create(dest) //nolint:gosec // operator-provided service config path\n\tif err != nil {\n\t\treturn err\n\t}\n\tok := false\n\tdefer func() {","sourceCodeStart":530,"sourceCodeEnd":566,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/cmd/cloudflared/linux_service.go#L530-L566","documentation":"After resolving the OpenRC template paths, uninstallOpenRC calls os.Remove on each service file. This error is returned when os.Remove fails for any reason other than the file not existing (ErrNotExist is deliberately tolerated so uninstall is idempotent). It signals a real filesystem obstacle such as missing permissions or the path being a non-empty directory.","triggerScenarios":"During `cloudflared service uninstall` on OpenRC, when os.Remove(path) on /etc/init.d/cloudflared or /etc/conf.d/cloudflared fails with EACCES/EPERM (not root, read-only filesystem, immutable file) or EISDIR/ENOTEMPTY (path is a directory).","commonSituations":"Running uninstall without sudo; /etc mounted read-only (recovery mode, immutable container image); file or parent directory marked immutable (chattr +i); SELinux/AppArmor denial on removing init scripts; leftover directory where the file used to be.","solutions":["Re-run with root privileges: `sudo cloudflared service uninstall`","Check and clear the immutable flag if set: `lsattr /etc/init.d/cloudflared; sudo chattr -i /etc/init.d/cloudflared`","If the filesystem is read-only, remount rw (`mount -o remount,rw /etc` or boot normally) and retry","Remove manually after fixing permissions: `sudo rm -f /etc/init.d/cloudflared /etc/conf.d/cloudflared`","Check the wrapped (%w) error for the exact errno (EACCES vs EISDIR etc.) to pick the right fix"],"exampleFix":"// before: non-root uninstall\n$ cloudflared service uninstall\n// error: error removing /etc/init.d/cloudflared: remove /etc/init.d/cloudflared: permission denied\n// after\n$ sudo cloudflared service uninstall","handlingStrategy":"try-catch","validationCode":"// pre-check writability before uninstall\nfunc ensureRemovable(path string) error {\n    if _, err := os.Stat(path); os.IsNotExist(err) {\n        return nil // nothing to remove\n    }\n    if err := syscall.Access(filepath.Dir(path), unix.W_OK); err != nil {\n        return fmt.Errorf(\"cannot remove %s: need write access to %s (run as root): %w\", path, filepath.Dir(path), err)\n    }\n    return nil\n}","typeGuard":"func fileRemovable(path string) bool {\n    fi, err := os.Stat(path)\n    if err != nil { return os.IsNotExist(err) } // absent = fine\n    return !fi.IsDir() && fi.Mode().Perm()&0o200 != 0 || os.Geteuid() == 0\n}","tryCatchPattern":"if err := uninstallLinuxService(c); err != nil {\n    if strings.Contains(err.Error(), \"error removing\") {\n        log.Warn(\"file removal blocked; check root privileges, read-only /etc, immutable flags (chattr -i), or SELinux denials\")\n    }\n    return err\n}","preventionTips":["Run uninstall as root; /etc is writable only by root on most systems","Check for immutable flags (lsattr/chattr -i) on /etc/init.d/cloudflared if removal fails with EPERM as root","Ensure /etc is not mounted read-only (containers, recovery mode) before uninstalling","Check SELinux/AppArmor audit logs if permission is denied despite root"],"tags":["linux","openrc","uninstall","permission-denied","file-removal"],"backgroundTag":"permission-denied","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"}