Freeing Disk Space – Docker and LLMs

If you’re like me, and especially in the age of large docker models and language models, your drive gets cluttered with files that take up a lot of space and are not really needed anymore (if you are the type of person who does most of your work on the cloud, then we don’t share the same local-first philosophy and this does not apply to you – or at least not the tools I am about to mention).

So to start, we need to find the obvious violators – for this I suggest the jaw-dropping fast WizTree software on Windows. If you are running a Mac you can try OmniDiskSweeper or DaisyDisk, though I haven’t tried them (neither have I tried QDirStat for Linux)

WizTree let’s you quickly find the space hogs and remove them. If you use local models served by specific applications like Ollama, then I’d suggest using the application that runs them to also delete them (if it has that functionality). In the case of Ollama, you can use the console to do stuff like:

ollama list

to view all models

and

ollama rm <modelname>

to remove the model you are not interested in anymore.

If you are using Unsloth Studio then you can set the location where the downloaded models are stored, but the default on Windows will be your huggingface cache directory (c:\Users\<userid>\.cache\huggingface\hub) where you will probably see that large repos taking up space in WizTree and delete whichever is not needed anymore

Another issue is Docker taking up lots of space. On Windows, Docker has a virtual disk at C:\Users\<userid>\AppData\Local\Docker\wsl\disk\docker_data.vhdx which only knows how to grow in size – it doesn’t decrease when you remove docker containers or images. Thus it can reach ridiculous proportions. I asked Gemini to generate a script that shrinks it back to the minimum necessary (just save the following as dockerspace.ps1 and run it):

# Requires -RunAsAdministrator

# 1. Automatically elevate the script to Administrator
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
    Write-Host "Requesting Administrator privileges..." -ForegroundColor Yellow
    # Relaunch PowerShell as Admin, bypassing execution policy, running this exact file
    Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
    Exit
}

Write-Host "=========================================================" -ForegroundColor Cyan
Write-Host " Starting Docker VHDX Deep Clean & Compaction Pipeline " -ForegroundColor Cyan
Write-Host "=========================================================" -ForegroundColor Cyan

# 2. Clean Docker internal storage
Write-Host "`n[1/5] Cleaning up Docker images, volumes, and cache..." -ForegroundColor Yellow
if (Get-Process -Name "Docker Desktop" -ErrorAction SilentlyContinue) {
    Write-Host "Docker is running. Triggering system prune..." -ForegroundColor Gray
    # Run the aggressive prune
    docker system prune -a --volumes -f
} else {
    Write-Host "Docker Desktop is not running. Skipping internal cleanup (cannot prune while stopped)." -ForegroundColor Red
    Write-Host "If you want to free up maximum space, start Docker, run this script, and let it handle the cleanup." -ForegroundColor Yellow
}

# 3. Trim the WSL Filesystem
Write-Host "`n[2/5] Forcing WSL to trim unused blocks..." -ForegroundColor Yellow
wsl --system -d docker-desktop fstrim -av

# 4. Stop Docker and WSL completely
Write-Host "`n[3/5] Stopping Docker Desktop and shutting down WSL..." -ForegroundColor Yellow
# Stop Docker Desktop app gracefully
Stop-Process -Name "Docker Desktop" -Force -ErrorAction SilentlyContinue
# Shut down WSL instance
wsl --shutdown
Start-Sleep -Seconds 3

# 5. Run Diskpart compaction
$vhdxPath = "C:\Users\amnon\AppData\Local\Docker\wsl\disk\docker_data.vhdx"

if (Test-Path $vhdxPath) {
    Write-Host "`n[4/5] Running Diskpart compaction on:" -ForegroundColor Yellow
    Write-Host "      $vhdxPath" -ForegroundColor Cyan
    
    # Create temporary diskpart script
    $tempScript = [System.IO.Path]::GetTempFileName()
    @("select vdisk file=""$vhdxPath""", "attach vdisk readonly", "compact vdisk", "detach vdisk") | Out-File -FilePath $tempScript -Encoding ascii
    
    # Execute diskpart
    diskpart /s $tempScript
    
    # Cleanup temp script
    Remove-Item $tempScript -ErrorAction SilentlyContinue
} else {
    Write-Error "Could not find the VHDX file at: $vhdxPath. Please double-check the path."
}

# 6. Complete!
Write-Host "`n[5/5] Done!" -ForegroundColor Yellow
Write-Host "=========================================================" -ForegroundColor Green
Write-Host " Success! The VHDX disk has been compacted." -ForegroundColor Green
Write-Host " You can now safely open Docker Desktop again." -ForegroundColor Green
Write-Host "=========================================================" -ForegroundColor Green

Read-Host -Prompt "Press Enter to exit"

Leave a Reply

Your email address will not be published. Required fields are marked *