Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to clean images, volumes and networks, hyper-v VMs and disk cache from degraded Azure CI system / agent
Write-Host "Waiting for containers to exit." -NoNewLine
while ($(docker ps -q))
Write-Host "." -NoNewLine
Start-Sleep 1
Write-Host "Stopping Azure Agent"
Stop-Service vstsagent*
# This cleaned up LCOW, but kills WSL2 VM, so skip it
# Write-Host "Killing all running VMs"
# hcsdiag list |
# ? { $_ -match '^[^\s]' } |
# % { Write-Host "Killing $_"; hcsdiag kill $_ }
$images = docker image ls -q | Measure-Object | Select -ExpandProperty Count
Write-Host "Found $images Images to Remove"
$volumes = docker volume ls -q | Measure-Object | Select -ExpandProperty Count
Write-Host "Found $volumes Volumes to Remove"
Write-Host "Removing all docker managed state"
docker system prune --all --volumes --force
Write-Host "Removing Docker builder cache"
docker builder prune --force
Write-Host "Stopping Docker"
Stop-Service Docker
Write-Host "Inspect WSL2 VDisk"
$vhd = Join-Path ([System.Environment]::GetFolderPath('LocalApplicationData')) "docker\wsl\data\ext4.vhdx"
$diskPartScript = [System.IO.Path]::GetTempFileName()
select vdisk file="$vhd"
detail vdisk
"@ | Out-File -FilePath $diskPartScript -Encoding Ascii
diskpart /s $diskPartScript
Write-Host "Compact WSL2 VDisk (skipped for now)"
select vdisk file="$vhd"
compact vdisk
detail vdisk
"@ | Out-File -FilePath $diskPartScript -Encoding Ascii
# diskpart /s $diskPartScript
Remove-Item $diskPartScript
Write-Host "Cleaning Docker LCOW file system caches"
# cache and scratch directories get recreated
) |
% {
$count = Get-ChildItem $_ -Recurse | Measure-Object | Select -ExpandProperty Count
Write-Host "Removing $count children from $_"
Remove-Item $_\* -Recurse -Force
Restart-Computer -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.