<# Claude 本地残留清理脚本(Windows) 用法(在 PowerShell 里,进入脚本所在文件夹后运行): powershell -ExecutionPolicy Bypass -File .\claude-cleanup-win.ps1 只扫描,不做任何改动 powershell -ExecutionPolicy Bypass -File .\claude-cleanup-win.ps1 -Delete 扫描后确认,再清理 清理方式:文件不会直接删除,而是移到 %USERPROFILE%\Claude-cleanup-时间戳\ 里; 注册表项会先导出成 .reg 备份放在同一个文件夹。确认没问题后再手动删掉这个文件夹。 只有"凭据管理器"里的条目删除后无法恢复,会单独再问一次。 不会动:项目文件夹里的 .claude\ 和 CLAUDE.md、浏览器里 claude.ai 的登录、 用户环境变量(只报告,不修改)。 #> param([switch]$Delete) $ErrorActionPreference = 'Continue' try { [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 } catch {} $U = $env:USERPROFILE $A = $env:APPDATA $LA = $env:LOCALAPPDATA $TS = Get-Date -Format 'yyyyMMdd-HHmmss' $Quarantine = Join-Path $U "Claude-cleanup-$TS" $DesktopDir = [Environment]::GetFolderPath('Desktop') $StartMenu = Join-Path $A 'Microsoft\Windows\Start Menu\Programs' function Ask-Yes([string]$Prompt) { $r = Read-Host "$Prompt 输入 yes 继续,其他任意键跳过" return ($r -eq 'yes') } function Get-SizeText([string]$Path) { try { $item = Get-Item -LiteralPath $Path -Force -ErrorAction Stop if ($item.PSIsContainer) { $sum = (Get-ChildItem -LiteralPath $Path -Recurse -Force -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum } else { $sum = $item.Length } if (-not $sum) { $sum = 0 } if ($sum -ge 1GB) { return '{0:N1}G' -f ($sum / 1GB) } if ($sum -ge 1MB) { return '{0:N1}M' -f ($sum / 1MB) } if ($sum -ge 1KB) { return '{0:N0}K' -f ($sum / 1KB) } return "$sum B" } catch { return '?' } } function Short([string]$Path) { if ($Path.StartsWith($U, [System.StringComparison]::OrdinalIgnoreCase)) { return '%USERPROFILE%' + $Path.Substring($U.Length) } return $Path } # ---------- 1. 收集候选路径 ---------- $patterns = @( # Claude Code(命令行) "$U\.claude", "$U\.claude.json", "$U\.claude.json.backup*", "$U\.config\claude", "$U\.local\bin\claude.exe", "$U\.local\share\claude", "$U\.local\state\claude", "$U\.cache\claude", "$LA\claude-cli-nodejs", # Claude 桌面 App "$A\Claude", "$A\Claude-3p", "$A\Anthropic", "$LA\AnthropicClaude", "$LA\Claude", "$LA\Anthropic", "$LA\Packages\Claude_*", # 快捷方式 "$StartMenu\Anthropic", "$StartMenu\Anthropic PBC", "$StartMenu\Claude.lnk", "$DesktopDir\Claude.lnk" ) $found = New-Object System.Collections.ArrayList foreach ($pat in $patterns) { Get-Item -Path $pat -Force -ErrorAction SilentlyContinue | ForEach-Object { [void]$found.Add($_.FullName) } } $regKeys = New-Object System.Collections.ArrayList $regCandidates = @( 'HKCU:\Software\Anthropic', 'HKCU:\Software\Classes\claude', 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\AnthropicClaude' ) foreach ($k in $regCandidates) { if (Test-Path -LiteralPath $k) { [void]$regKeys.Add($k) } } # 浏览器扩展的本地桥接(Chrome / Edge / Brave) $nmhRoots = @( 'HKCU:\Software\Google\Chrome\NativeMessagingHosts', 'HKCU:\Software\Microsoft\Edge\NativeMessagingHosts', 'HKCU:\Software\BraveSoftware\Brave-Browser\NativeMessagingHosts' ) foreach ($root in $nmhRoots) { if (Test-Path -LiteralPath $root) { Get-ChildItem -LiteralPath $root -ErrorAction SilentlyContinue | Where-Object { $_.PSChildName -like 'com.anthropic.*' } | ForEach-Object { [void]$regKeys.Add("$root\$($_.PSChildName)") } } } # 开机自启动项 $runKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run' $runValues = @() if (Test-Path -LiteralPath $runKey) { $props = Get-ItemProperty -LiteralPath $runKey $runValues = @($props.PSObject.Properties | Where-Object { $_.Name -notlike 'PS*' -and ($_.Name -match 'claude|anthropic' -or "$($_.Value)" -match 'claude|anthropic') } | ForEach-Object { $_.Name }) } # ---------- 2. 扫描并报告 ---------- Write-Host '======================================' Write-Host ' Claude 本地残留扫描(Windows)' Write-Host '======================================' Write-Host '' Write-Host '[文件和文件夹]' if ($found.Count -eq 0) { Write-Host ' 没有找到。' } foreach ($p in $found) { Write-Host (' {0,-8} {1}' -f (Get-SizeText $p), (Short $p)) } Write-Host '' Write-Host '[注册表]' if ($regKeys.Count -eq 0 -and $runValues.Count -eq 0) { Write-Host ' 没有找到。' } foreach ($k in $regKeys) { Write-Host " $k" } foreach ($v in $runValues) { Write-Host " $runKey -> $v(开机自启动)" } Write-Host '' Write-Host '[安装方式]' $npmInstalled = $false if (Get-Command npm -ErrorAction SilentlyContinue) { & npm ls -g '@anthropic-ai/claude-code' --depth=0 *> $null if ($LASTEXITCODE -eq 0) { $npmInstalled = $true Write-Host ' npm 全局包 @anthropic-ai/claude-code:已安装' } } $appx = @(Get-AppxPackage -ErrorAction SilentlyContinue | Where-Object { $_.Publisher -like '*Anthropic*' }) foreach ($pkg in $appx) { Write-Host " 应用商店 / MSIX 版:$($pkg.Name) $($pkg.Version)" } $squirrel = Join-Path $LA 'AnthropicClaude\Update.exe' $hasSquirrel = Test-Path -LiteralPath $squirrel if ($hasSquirrel) { Write-Host ' 官网安装版(AnthropicClaude):已安装' } if (-not $npmInstalled -and $appx.Count -eq 0 -and -not $hasSquirrel) { Write-Host ' 没有发现 npm / MSIX / 官网安装版。' } Write-Host '' Write-Host '[凭据管理器]' $credTargets = @() $cmdkeyOut = & cmdkey /list 2>$null foreach ($line in $cmdkeyOut) { if ($line -match '^\s*(Target|目标)\s*[::]\s*(.+)$') { $t = $Matches[2].Trim() if ($t -match 'claude|anthropic') { # cmdkey 显示为 "LegacyGeneric:target=xxx",删除时要用 target= 后面的部分 if ($t -match 'target=(.+)$') { $t = $Matches[1] } $credTargets += $t } } } if ($credTargets.Count -eq 0) { Write-Host ' 没有找到。' } foreach ($t in $credTargets) { Write-Host " $t" } Write-Host '' Write-Host '[正在运行的进程]' $procs = @(Get-Process -ErrorAction SilentlyContinue | Where-Object { $_.ProcessName -eq 'claude' -or $_.ProcessName -eq 'Claude' }) if ($procs.Count -eq 0) { Write-Host ' 没有。' } foreach ($pr in $procs) { Write-Host " $($pr.ProcessName) (PID $($pr.Id))" } Write-Host '' Write-Host '[只报告、不会修改的项目]' $envNames = @() foreach ($scope in 'User', 'Machine') { $vars = [Environment]::GetEnvironmentVariables($scope) foreach ($name in $vars.Keys) { if ($name -match '^(ANTHROPIC_|CLAUDE_)') { $envNames += "$name($scope)" } } } if ($envNames.Count -eq 0) { Write-Host ' 没有。' } else { Write-Host ' 有这些环境变量(只显示名字,不显示值),请在"系统属性 > 环境变量"里手动删除:' foreach ($n in $envNames) { Write-Host " $n" } } Write-Host '' if (-not $Delete) { Write-Host '以上只是扫描,什么都没改。' Write-Host '要清理,请运行:powershell -ExecutionPolicy Bypass -File .\claude-cleanup-win.ps1 -Delete' exit 0 } # ---------- 3. 清理 ---------- if ($found.Count -eq 0 -and $regKeys.Count -eq 0 -and $runValues.Count -eq 0 -and -not $npmInstalled -and $appx.Count -eq 0 -and -not $hasSquirrel -and $credTargets.Count -eq 0) { Write-Host '没有需要清理的内容。' exit 0 } Write-Host '提醒:清理前如需保留会话记录或配置,请先自行备份 %USERPROFILE%\.claude 里需要的内容。' Write-Host '' if ($procs.Count -gt 0) { if (Ask-Yes 'Claude 正在运行,需要先退出。是否自动结束这些进程?') { $procs | Stop-Process -Force -ErrorAction SilentlyContinue Start-Sleep -Seconds 2 } else { Write-Host '请先手动退出 Claude 桌面 App 和所有 claude 终端会话,再重新运行。' exit 1 } } if (-not (Ask-Yes "将卸载上面列出的安装,把文件移到 %USERPROFILE%\Claude-cleanup-$TS\,并删除注册表项(先导出备份)。")) { Write-Host '已取消,什么都没改。' exit 0 } New-Item -ItemType Directory -Path $Quarantine -Force | Out-Null if ($npmInstalled) { Write-Host '卸载 npm 包 @anthropic-ai/claude-code ...' & npm uninstall -g '@anthropic-ai/claude-code' if ($LASTEXITCODE -ne 0) { Write-Host ' npm 卸载失败,请稍后手动运行:npm uninstall -g @anthropic-ai/claude-code' } } foreach ($pkg in $appx) { Write-Host "卸载 $($pkg.Name) ..." try { Remove-AppxPackage -Package $pkg.PackageFullName -ErrorAction Stop } catch { Write-Host " 卸载失败:$($_.Exception.Message)" } } if ($hasSquirrel) { Write-Host '卸载官网安装版 Claude ...' try { Start-Process -FilePath $squirrel -ArgumentList '--uninstall', '-s' -Wait -ErrorAction Stop Start-Sleep -Seconds 2 } catch { Write-Host " 卸载程序运行失败:$($_.Exception.Message)" } } $moved = 0 $failed = @() foreach ($p in $found) { if (-not (Test-Path -LiteralPath $p)) { continue } # 可能已被上面的卸载步骤删掉 $name = ($p -replace ':', '') -replace '\\', '__' $dest = Join-Path $Quarantine $name try { Move-Item -LiteralPath $p -Destination $dest -Force -ErrorAction Stop Write-Host " 已移走:$(Short $p)" $moved++ } catch { # 跨磁盘时 Move-Item 移动文件夹会失败,改为复制后删除 try { Copy-Item -LiteralPath $p -Destination $dest -Recurse -Force -ErrorAction Stop Remove-Item -LiteralPath $p -Recurse -Force -ErrorAction Stop Write-Host " 已移走:$(Short $p)" $moved++ } catch { $failed += $p } } } $regDone = 0 foreach ($k in $regKeys) { if (-not (Test-Path -LiteralPath $k)) { continue } $regPath = $k -replace '^HKCU:\\', 'HKCU\' $backup = Join-Path $Quarantine ("reg__" + (($k -replace '[:\\ ]', '_')) + '.reg') & reg export $regPath $backup /y *> $null try { Remove-Item -LiteralPath $k -Recurse -Force -ErrorAction Stop Write-Host " 已删除注册表项:$k(备份已导出)" $regDone++ } catch { $failed += $k } } if ($runValues.Count -gt 0) { & reg export 'HKCU\Software\Microsoft\Windows\CurrentVersion\Run' (Join-Path $Quarantine 'reg__Run.reg') /y *> $null foreach ($v in $runValues) { try { Remove-ItemProperty -LiteralPath $runKey -Name $v -ErrorAction Stop Write-Host " 已删除开机自启动项:$v" $regDone++ } catch { $failed += "$runKey -> $v" } } } if ($credTargets.Count -gt 0) { Write-Host '' if (Ask-Yes '删除凭据管理器里的 Claude 条目(删除后无法恢复,需要重新登录)?') { foreach ($t in $credTargets) { & cmdkey "/delete:$t" *> $null Write-Host " 已删除凭据:$t" } } else { Write-Host ' 已跳过凭据管理器。' } } Write-Host '' Write-Host '======================================' Write-Host ' 完成' Write-Host '======================================' Write-Host "移走了 $moved 项文件,删除了 $regDone 项注册表,备份存放在:%USERPROFILE%\Claude-cleanup-$TS\" Write-Host '确认一切正常后,可以手动删除这个文件夹。它里面可能有登录信息,不要发给别人。' if ($failed.Count -gt 0) { Write-Host '' Write-Host '以下项目处理失败(可能被占用或需要管理员权限),重启电脑后可再运行一次,或手动删除:' foreach ($f in $failed) { Write-Host " $f" } } if ($envNames.Count -gt 0) { Write-Host '' Write-Host '别忘了手动删除上面列出的环境变量,改完重开终端。' } if ((Get-ChildItem -LiteralPath $Quarantine -Force | Measure-Object).Count -eq 0) { Remove-Item -LiteralPath $Quarantine -Force } exit 0