How to reduce size of docker data volume in Docker Desktop for Windows v2
Marc Ziel

Marc Ziel @marzelin

Joined:
Apr 30, 2018

How to reduce size of docker data volume in Docker Desktop for Windows v2

Publish Date: Nov 8 '20
40 9

Docker Desktop for Windows v2, which uses WSL2, stores all image and container files in a separate virtual volume (vhdx). This virtual hard disk file can automatically grow when it needs more space (to a certain limit). Unfortunately, if you reclaim some space, i.e. by removing unused images, vhdx doesn't shrink automatically. Luckily, you can reduce its size manually by calling this command in PowerShell (as Administrator):

Optimize-VHD -Path c:\path\to\data.vhdx -Mode Full
Enter fullscreen mode Exit fullscreen mode

The docker-desktop-data vhdx is normally in this location:

%LOCALAPPDATA%\Docker\wsl\data\ext4.vhdx
Enter fullscreen mode Exit fullscreen mode

Comments 9 total

  • Zvi Cahana
    Zvi CahanaMar 15, 2021

    Thanks for this useful info!

    Note that both Docker Desktop and WSL2 need to be stopped before running this, otherwise an error will occur:

    Optimize-VHD : Failed to compact the virtual disk.
    The system failed to compact 'C:\Users\ZVICAHANA\AppData\Local\Docker\wsl\data\ext4.vhdx'.
    Failed to compact the virtual disk.
    The system failed to compact 'C:\Users\ZVICAHANA\AppData\Local\Docker\wsl\data\ext4.vhdx': The process cannot access
    the file because it is being used by another process. (0x80070020).
    At line:1 char:1
    + Optimize-VHD -Path C:\Users\ZVICAHANA\AppData\Local\Docker\wsl\data\e ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceBusy: (:) [Optimize-VHD], VirtualizationException
        + FullyQualifiedErrorId : ObjectInUse,Microsoft.Vhd.PowerShell.Cmdlets.OptimizeVhd```
    
    
    
    
    
    Enter fullscreen mode Exit fullscreen mode

    To stop Docker Desktop, right click its tray icon and choose "Quit Docker Desktop".
    To stop WSL2, open a command prompt and run wsl --shutdown.

  • Dan Manastireanu
    Dan ManastireanuApr 2, 2021

    Thanks, reclaimed 14.5 GB of my ssd :D

  • Little Red Riding Hood
    Little Red Riding HoodJun 8, 2021

    Thanks, it was really useful for me!

  • István Földházi
    István FöldháziAug 4, 2021

    One-liner:

    Optimize-VHD -Path $Env:LOCALAPPDATA\Docker\wsl\data\ext4.vhdx -Mode Full
    
    Enter fullscreen mode Exit fullscreen mode

    If the above command fails with "The process cannot access the file because it is being used by another process.", stop services and tasks using that file:

    net stop com.docker.service
    taskkill /IM "docker.exe" /F
    taskkill /IM "Docker Desktop.exe" /F
    wsl --shutdown
    
    Enter fullscreen mode Exit fullscreen mode
  • John Bevan
    John BevanJan 11, 2022

    Thanks for sharing this.
    For anyone who doesn't have this cmdlet / the Hyper-V module, you can add it via Add/Remove Programs, Windows Features, per: superuser.com/a/1307442/156700

  • Barbakadze
    BarbakadzeNov 1, 2022

    I get an error: Optimize-VHD : 'Local\Docker\data\ext4.vhdx4' is not an existing virtual hard disk file.
    Although vhdx4 file is right there.

  • Mateusz
    MateuszDec 12, 2022

    If you get error:

    Optimize-VHD : Hyper-V encountered an error trying to access an object on computer 'K48689' because the object was not
    found. The object might have been deleted. Verify that the Virtual Machine Management service on the computer is runnin
    g.
    
    Enter fullscreen mode Exit fullscreen mode

    Run command:

    mofcomp %SYSTEMROOT%\System32\WindowsVirtualization.V2.mof
    
    Enter fullscreen mode Exit fullscreen mode
  • Kiquenet
    KiquenetApr 1, 2025

    I use WSL in Windows 11
    I dont use Docker Desktop, only docker service in WSL

    wsl --shutdown
    Optimize-VHD -Path C:\Users\myuser\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu24.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx -Mode Full -Verbose

    I get "The process cannot access the file because it is being used by another process. Error code: Wsl/0x80070020"

    handle / handle64 sysinternal no matching handles found
    handle64 -a C:\Users\myuser\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu24.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx

    no matching handles found

Add comment