{"record":{"id":"83cdd967d1144f04","repo":"hashicorp/nomad","slug":"cannot-remove-existing-nomad-binary-install-s","errorCode":null,"errorMessage":"cannot remove existing nomad binary install - %s","messagePattern":"cannot remove existing nomad binary install - (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/windows_service_install.go","lineNumber":357,"sourceCode":"\tdefer exeFile.Close()\n\n\t// Copy into a temporary file which can then be moved\n\t// into the correct location.\n\tdstFile, err := os.CreateTemp(os.TempDir(), \"nomad*\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"cannot create copy - %s\", err)\n\t}\n\tdefer dstFile.Close()\n\n\tif _, err = io.Copy(dstFile, exeFile); err != nil {\n\t\treturn fmt.Errorf(\"cannot write nomad binary for install - %s\", err)\n\t}\n\tdstFile.Close()\n\n\t// With a copy ready to be moved into place, ensure that\n\t// the path is clear then move the file.\n\tif err = os.RemoveAll(opts.binaryPath); err != nil {\n\t\treturn fmt.Errorf(\"cannot remove existing nomad binary install - %s\", err)\n\t}\n\n\tif err = os.Rename(dstFile.Name(), opts.binaryPath); err != nil {\n\t\treturn fmt.Errorf(\"cannot install new nomad binary - %s\", err)\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":339,"sourceCodeEnd":366,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/windows_service_install.go#L339-L366","documentation":"binaryInstall wraps the error from os.RemoveAll(opts.binaryPath), which clears the existing installation path before the new binary is renamed into place. If the old binary cannot be removed, the install stops to avoid overwriting a locked or protected file.","triggerScenarios":"os.RemoveAll on the configured binary path fails because the running Nomad Windows service has the exe locked, a file-permission/ACL denies deletion, or the file is held by antivirus while performInstall is executing.","commonSituations":"Upgrading an existing Nomad service without stopping it first; installing over a path owned by another user; exe locked by an EDR scanner.","solutions":["Stop the nomad Windows service (or uninstall it) before running the installer","Run the installer elevated as an account with permission to delete the target path","Check the wrapped error for a sharing violation and identify the locking process (e.g. antivirus)","Delete the old binary manually and re-run the install"],"exampleFix":"// before\nnomad windows service install   # service still running, exe locked\n// after\nnet stop nomad\nnomad windows service install","handlingStrategy":"validation","validationCode":"if _, err := os.Stat(binaryPath); err == nil {\n    if err := syscall.Chmod(binaryPath, 0o666); err != nil { // or stop service first\n        return fmt.Errorf(\"cannot touch existing binary: %v\", err)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := installCmd.Run(); err != nil {\n    if strings.Contains(err.Error(), \"cannot remove existing nomad binary\") {\n        // stop the nomad service, then retry\n    }\n}","preventionTips":["Stop the nomad service before upgrading the binary","Run the installer with administrator privileges","Check for processes holding the exe open (handle.exe / Process Explorer)"],"tags":["go","windows","filesystem","file-locked"],"backgroundTag":"file-in-use-cannot-delete","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"}