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```
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks for this useful info!
Note that both Docker Desktop and WSL2 need to be stopped before running this, otherwise an error will occur:
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
.