{"record":{"id":"a1945e0fefb1800a","repo":"hashicorp/nomad","slug":"could-not-get-existing-service-w","errorCode":null,"errorMessage":"could not get existing service - %w","messagePattern":"could not get existing service - %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/windows_service_uninstall.go","lineNumber":102,"sourceCode":"\tc.Ui.Info(\"Successfully uninstalled nomad Windows service\")\n\treturn 0\n}\n\nfunc (c *WindowsServiceUninstallCommand) performUninstall(m winsvc.WindowsServiceManager) error {\n\t// Check that the nomad service is currently registered\n\texists, err := m.IsServiceRegistered(winsvc.WINDOWS_SERVICE_NAME)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unable to check for existing service - %w\", err)\n\t}\n\n\tif !exists {\n\t\treturn nil\n\t}\n\n\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","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/windows_service_uninstall.go#L84-L120","documentation":"performUninstall wraps the error from m.GetService(WINDOWS_SERVICE_NAME) after confirming the service is registered. It means the handle to the existing service could not be opened, typically due to insufficient SCM access rights, so the uninstall cannot proceed to stop/delete the service.","triggerScenarios":"GetService fails because the process lacks SERVICE_QUERY_STATUS/QUERY_CONFIG rights (non-elevated run), the service record disappeared between the existence check and the lookup, or SCM access is denied by policy.","commonSituations":"Uninstalling from a non-admin shell; the service was deleted concurrently by another process; security software restricting SCM handles.","solutions":["Re-run the uninstall from an elevated Administrator prompt","Check the wrapped error code (ERROR_ACCESS_DENIED implies elevation needed)","Verify the service still exists with `sc query nomad` and retry","Reboot or restart SCM if the service record is in an inconsistent state"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := uninstallCmd.Run(); err != nil {\n    if strings.Contains(err.Error(), \"could not get existing service\") {\n        // check `sc query nomad`; re-run elevated\n    }\n}","preventionTips":["Run uninstalls elevated","Avoid concurrent scripts deleting the same service","Confirm the service exists with `sc query nomad` before uninstalling"],"tags":["go","windows","service-control-manager","permissions"],"backgroundTag":"windows-service-access-denied","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"}