param() $ErrorActionPreference = "Stop" $ProgressPreference = "SilentlyContinue" $script:Stage = "init" $script:LogFile = Join-Path $env:TEMP ("openclaw-uninstall-" + (Get-Date -Format "yyyyMMdd-HHmmss") + ".log") try { Start-Transcript -Path $script:LogFile -Force | Out-Null } catch {} function Log($msg) { Write-Host "`n==> $msg" -ForegroundColor Cyan } function Warn($msg) { Write-Warning $msg } function Get-NpmCmd { $cmd = Get-Command npm.cmd -ErrorAction SilentlyContinue if ($cmd) { return $cmd.Source } $fallback = "C:\Program Files\nodejs\npm.cmd" if (Test-Path $fallback) { return $fallback } return $null } function Get-OpenClawCmd { $cmd = Get-Command openclaw.cmd -ErrorAction SilentlyContinue if ($cmd) { return $cmd.Source } $candidate = Join-Path $env:APPDATA "npm\openclaw.cmd" if (Test-Path $candidate) { return $candidate } return $null } function Uninstall-OpenClaw { $script:Stage = "uninstall-openclaw" $openclaw = Get-OpenClawCmd if ($openclaw) { Log "Removing Gateway service, state, and workspace" try { & $openclaw uninstall --all --yes --non-interactive if ($LASTEXITCODE -ne 0) { throw "openclaw uninstall exit code: $LASTEXITCODE" } } catch { Warn "openclaw uninstall failed: $($_.Exception.Message)" } } else { Warn "openclaw.cmd not found, skipping built-in uninstall" } $npm = Get-NpmCmd if ($npm) { Log "Removing global openclaw CLI" try { & $npm uninstall -g openclaw if ($LASTEXITCODE -ne 0) { throw "npm uninstall exit code: $LASTEXITCODE" } } catch { Warn "npm uninstall -g openclaw failed: $($_.Exception.Message)" } } else { Warn "npm.cmd not found, skipping CLI removal" } } function Main { Log "Log file: $script:LogFile" Uninstall-OpenClaw $script:Stage = "done" Log "Uninstall finished" Write-Host "OpenClaw uninstall steps are complete. Node was left installed." -ForegroundColor Green } try { Main exit 0 } catch { Write-Error "[FAILED] stage=$script:Stage`n$($_.Exception.Message)" Write-Host "`nLog file: $script:LogFile" -ForegroundColor Yellow exit 1 } finally { try { Stop-Transcript | Out-Null } catch {} }