{"record":{"id":"6d11845a8de496f7","repo":"hashicorp/nomad","slug":"could-not-delete-service-w","errorCode":null,"errorMessage":"could not delete service - %w","messagePattern":"could not delete service - %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/windows_service_uninstall.go","lineNumber":117,"sourceCode":"\t// Grab the service and ensure the service is stopped\n\tsrvc, err := m.GetService(winsvc.WINDOWS_SERVICE_NAME)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"could not get existing service - %w\", err)\n\t}\n\tdefer srvc.Close()\n\n\tif err := srvc.Stop(); err != nil {\n\t\treturn fmt.Errorf(\"unable to stop service - %w\", err)\n\t}\n\n\t// Remove the service from the event log\n\tif err := srvc.DisableEventlog(); err != nil {\n\t\treturn fmt.Errorf(\"could not remove eventlog configuration - %w\", err)\n\t}\n\n\t// Finally, delete the service\n\tif err := srvc.Delete(); err != nil {\n\t\treturn fmt.Errorf(\"could not delete service - %w\", err)\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":99,"sourceCodeEnd":122,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/windows_service_uninstall.go#L99-L122","documentation":"performUninstall wraps the error from srvc.Delete(), the final step that marks the Nomad service for removal from the SCM. If the service cannot be deleted, it remains registered and the uninstall fails. The service has already been stopped and its eventlog disabled at this point.","triggerScenarios":"srvc.Delete() fails due to lack of DELETE permission on the service (non-elevated run), the service is marked for removal but a handle is still open, or the SCM rejects deletion because the process holds open service handles.","commonSituations":"Uninstall from non-admin shell; another tool (service viewer, EDR) holding an open handle to the service; service pending deletion after a prior failed uninstall.","solutions":["Run the uninstall elevated; check the wrapped error code for ERROR_ACCESS_DENIED","Close other tools holding handles to the service (sc query, service viewers) and retry","If the service is marked DELETE_PENDING, reboot to complete removal, then verify with `sc query nomad`","Fall back to `sc delete nomad` manually after fixing permissions"],"exampleFix":"# verify and manual fallback\nsc query nomad\nsc delete nomad","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"err := uninstallCmd.Run()\nif err != nil && strings.Contains(err.Error(), \"could not delete service\") {\n    // close other SCM handles, then retry once; reboot if DELETE_PENDING\n    _ = exec.Command(\"sc\", \"delete\", \"nomad\").Run()\n}","preventionTips":["Run uninstalls elevated","Avoid running `sc query`/service viewers concurrently with uninstall","Verify deletion with `sc query nomad` returning 1060 (not installed)"],"tags":["go","windows","service-control-manager","service-delete"],"backgroundTag":"windows-service-delete-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}