-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-purge.ps1
More file actions
63 lines (47 loc) · 1.85 KB
/
docker-purge.ps1
File metadata and controls
63 lines (47 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Get Docker disk space info
Write-Host "DISK SPACE USAGE BY DOCKER" -ForegroundColor DarkGreen
docker system df
Start-Sleep -Seconds 1
Write-Host ""
# Delete everything, supposedly :/
Write-Host "Stopping and removing all containers..." -ForegroundColor Yellow
docker rm -f $(docker ps -aq)
Start-Sleep -Seconds 1
Write-Host "`nPruning all containers..." -ForegroundColor Yellow
docker container prune -f
Start-Sleep -Seconds 1
Write-Host "`nPruning all images..." -ForegroundColor Yellow
docker image prune -af
Start-Sleep -Seconds 1
Write-Host "`nPruning all volumes..." -ForegroundColor Yellow
docker volume prune -af
Start-Sleep -Seconds 1
Write-Host "`nPruning all builds..." -ForegroundColor Yellow
docker builder prune -af
Start-Sleep -Seconds 1
Write-Host "`nPruning all objects..." -ForegroundColor Yellow
docker system prune --volumes -af
Start-Sleep -Seconds 1
# Closing Docker Desktop & WSL
Write-Host "`nClosing Docker Desktop...`n" -ForegroundColor Cyan
Get-Process | Where-Object {$_.Name -like "*docker*"} | Stop-Process -Force
Write-Host "Shutting down WSL...`n" -ForegroundColor Cyan
wsl --shutdown
# Compacting docker_data.vhdx with diskpart
Write-Host "Launching Diskpart as Administrator...`n" -ForegroundColor Cyan
$DiskPartScriptPath = "$env:Temp\diskpart_commands.txt"
$DiskPartCommands = @"
select vdisk file="%USERPROFILE%\AppData\Local\Docker\wsl\disk\docker_data.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit
"@
Set-Content -Path $DiskPartScriptPath -Value $DiskPartCommands
Start-Process powershell -Verb RunAs -ArgumentList '-Command', 'diskpart /s $env:Temp\diskpart_commands.txt; Start-Sleep -Seconds 3' -Wait
Remove-Item -Path $DiskPartScriptPath -Force
# Show completion message
Write-Host "`nDONE!" -ForegroundColor Green
# Show exit confirmation
Write-Host "`nPress Enter to exit" -ForegroundColor Blue -NoNewLine
Read-Host