{"record":{"id":"d81b29d33b728908","repo":"cloudflare/cloudflared","slug":"agent-service-s-is-not-installed-so-it-could-not","errorCode":null,"errorMessage":"agent service %s is not installed, so it could not be uninstalled","messagePattern":"agent service (.+?) is not installed, so it could not be uninstalled","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/cloudflared/windows_service.go","lineNumber":393,"sourceCode":"\n\tlog.Info().Msg(\"Agent service for cloudflared installed successfully\")\n\treturn nil\n}\n\nfunc uninstallWindowsService(c *cli.Context) error {\n\tlog := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog).\n\t\tWith().\n\t\tStr(LogFieldWindowsServiceName, windowsServiceName).Logger()\n\n\tlog.Info().Msg(\"Uninstalling cloudflared agent service\")\n\tm, err := mgr.Connect()\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Cannot establish a connection to the service control manager\")\n\t}\n\tdefer m.Disconnect()\n\ts, err := m.OpenService(windowsServiceName)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"agent service %s is not installed, so it could not be uninstalled\", windowsServiceName)\n\t}\n\tdefer s.Close()\n\n\tif status, err := s.Query(); err == nil && status.State == svc.Running {\n\t\tlog.Info().Msg(\"Stopping cloudflared agent service\")\n\t\tif _, err := s.Control(svc.Stop); err != nil {\n\t\t\tlog.Info().Err(err).Msg(\"Failed to stop cloudflared agent service, you may need to stop it manually to complete uninstall.\")\n\t\t}\n\t}\n\n\terr = s.Delete()\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Cannot delete agent service\")\n\t}\n\tlog.Info().Msg(\"Agent service for cloudflared was uninstalled successfully\")\n\terr = eventlog.Remove(windowsServiceName)\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Cannot remove event logger\")","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/cloudflare/cloudflared/blob/2253eeeb25a44a713a4b60b8ba1e1b3f377d1a0f/cmd/cloudflared/windows_service.go#L375-L411","documentation":"Returned by uninstallWindowsService when the Windows Service Control Manager connection succeeds but OpenService cannot find the cloudflared agent service. It means there is nothing installed under the expected service name, so the uninstall request cannot proceed.","triggerScenarios":"Running `cloudflared service uninstall` (uninstallWindowsService) when the service was never installed, was already uninstalled, or was installed under a different service name (windowsServiceName mismatch).","commonSituations":"Double-uninstall in a teardown script; installing via a different binary/name (e.g. legacy service name) and trying to remove it with the new name; uninstalling on a machine where the install step failed earlier.","solutions":["Verify the service exists with `sc query cloudflared` and match the exact service name before uninstalling.","If the goal is idempotent teardown, treat a missing service as success and skip the uninstall step.","If the service was installed under a different name, uninstall using the binary that installed it or `sc delete <name>`."],"exampleFix":"// before: blind uninstall fails when absent\ncloudflared service uninstall\n// after: idempotent guard in a script\nsc query cloudflared >nul 2>&1 && cloudflared service uninstall || echo service not installed, skipping","handlingStrategy":"validation","validationCode":"// Go: check service existence before uninstalling\nm, err := mgr.Connect()\nif err != nil { return err }\ns, err := m.OpenService(windowsServiceName)\nif err != nil {\n    // treat as already-uninstalled\n    return nil\n}\ns.Close()","typeGuard":null,"tryCatchPattern":"if err := uninstallWindowsService(); err != nil {\n    if strings.Contains(err.Error(), \"is not installed\") { return nil /* idempotent */ }\n    return err\n}","preventionTips":["Make uninstall steps idempotent in scripts","Query `sc query <service>` before uninstall","Keep the installing and uninstalling binary versions in sync so the service name matches"],"tags":["windows","service-control-manager","uninstall"],"backgroundTag":"entity-not-found","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"}