{"id":11477,"date":"2026-11-12T17:00:51","date_gmt":"2026-11-12T17:00:51","guid":{"rendered":"https:\/\/cheapwindowsvps.com\/blog\/how-to-shrink-wsl2-and-docker-desktop-vhdx-files-on-windows\/"},"modified":"2026-11-12T17:00:51","modified_gmt":"2026-11-12T17:00:51","slug":"how-to-shrink-wsl2-and-docker-desktop-vhdx-files-on-windows","status":"publish","type":"post","link":"http:\/\/cheapwindowsvps.com\/blog\/how-to-shrink-wsl2-and-docker-desktop-vhdx-files-on-windows\/","title":{"rendered":"How to Shrink WSL2 and Docker Desktop VHDX Files on Windows"},"content":{"rendered":"<p>WSL2 and Docker Desktop store their data in dynamically expanding VHDX files. These files grow as data is added, but deleting files inside Linux or Docker does not automatically reduce the space they occupy on the Windows drive.<\/p>\n<p>You can reclaim that space by cleaning up unused data and compacting the VHDX file.<\/p>\n<h2>Compact a WSL2 virtual disk<\/h2>\n<p>First, check how much space is in use inside the Linux distribution:<\/p>\n<pre><code class=\"language-bash\">df -h \/<\/code><\/pre>\n<p>To find the distribution name, run this in Windows:<\/p>\n<pre><code class=\"language-powershell\">wsl.exe --list --verbose<\/code><\/pre>\n<p>You can then locate its VHDX file using PowerShell. Replace <code>Ubuntu-24.04<\/code> with your distribution\u2019s name:<\/p>\n<pre><code class=\"language-powershell\">$wsl2distro = 'Ubuntu-24.04'$VHDX = (Get-ChildItem -Path HKCU:SoftwareMicrosoftWindowsCurrentVersionLxss |    Where-Object { $_.GetValue(&quot;DistributionName&quot;) -eq $wsl2distro }).GetValue(&quot;BasePath&quot;) + &quot;ext4.vhdx&quot;<\/code><\/pre>\n<p>Before compacting the disk, remove unnecessary files from the distribution. The <code>ncdu<\/code> utility can help identify large files and directories:<\/p>\n<pre><code class=\"language-bash\">sudo apt-get install ncdusudo ncdu --exclude \/mnt<\/code><\/pre>\n<p>You can also remove unused packages and cached files on Ubuntu or Debian:<\/p>\n<pre><code class=\"language-bash\">sudo apt-get update &amp;&amp; sudo apt-get autoremove -y &amp;&amp; sudo apt-get clean<\/code><\/pre>\n<p>Shut down WSL before compacting the VHDX:<\/p>\n<pre><code class=\"language-powershell\">wsl --shutdownwsl.exe --list --verbose<\/code><\/pre>\n<p>Make sure all distributions are stopped. Then use either <code>Optimize-VHD<\/code> or DiskPart to compact the file.<\/p>\n<p>With <code>Optimize-VHD<\/code>:<\/p>\n<pre><code class=\"language-powershell\">Optimize-VHD -Path &quot;C:pathtoext4.vhdx&quot; -Mode Full<\/code><\/pre>\n<p>The <code>Optimize-VHD<\/code> cmdlet requires the Hyper-V management tools, which are available on Windows Pro and Enterprise editions.<\/p>\n<p>Alternatively, use DiskPart:<\/p>\n<pre><code class=\"language-text\">diskpartselect vdisk file=&quot;C:pathtoext4.vhdx&quot;attach vdisk readonlycompact vdiskdetach vdiskexit<\/code><\/pre>\n<p>Afterward, check the VHDX file\u2019s size on the Windows drive. It should be closer to the amount of data actually stored in the Linux distribution. You can also <a target=\"_blank\" rel=\"nofollow noopener\" href=\"https:\/\/woshub.com\/move-wsl-another-drive-windows\/\">move a WSL distribution to another drive<\/a> if you want to free space on the system volume.<\/p>\n<h2>Reclaim Docker Desktop disk space<\/h2>\n<p>Docker Desktop also stores data in a WSL2 VHDX file. Check Docker\u2019s disk usage with:<\/p>\n<pre><code class=\"language-powershell\">docker system df<\/code><\/pre>\n<p>Remove unused Docker images and other data first:<\/p>\n<pre><code class=\"language-powershell\">docker system prune -a<\/code><\/pre>\n<p>Docker\u2019s WSL data is typically stored at:<\/p>\n<pre><code class=\"language-text\">%LOCALAPPDATA%Dockerwsldiskdocker_data.vhdx<\/code><\/pre>\n<p>Close Docker Desktop, shut down WSL, then compact the file with one of the methods above. For example:<\/p>\n<pre><code class=\"language-powershell\">wsl --shutdownOptimize-VHD -Path &quot;$env:LOCALAPPDATADockerwsldiskdocker_data.vhdx&quot; -Mode Full<\/code><\/pre>\n<h2>Automatic space reclamation with sparse mode<\/h2>\n<p>WSL2 has an experimental sparse VHD feature that can return unused space to Windows as data is deleted. You can enable it for a distribution with:<\/p>\n<pre><code class=\"language-powershell\">wsl --manage Ubuntu-24.04 --set-sparse true --allow-unsafe<\/code><\/pre>\n<p>The <code>--allow-unsafe<\/code> option is required when WSL warns that sparse VHD support may cause data corruption. Because the feature is experimental and carries a risk to both the Windows NTFS volume and the guest ext4 filesystem, it is not recommended for production systems.<\/p>\n<p>You can also enable sparse mode in <code>%UserProfile%.wslconfig<\/code>:<\/p>\n<pre><code class=\"language-ini\">[experimental]sparseVhd=true<\/code><\/pre>\n<p>Compact the VHDX with DiskPart or <code>Optimize-VHD<\/code> before enabling sparse mode. Afterward, those tools can no longer be used to compact that file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WSL2 and Docker Desktop store their data in dynamically expanding VHDX files. These files grow as data is added, but deleting files inside Linux or Docker does not\u2026<\/p>\n","protected":false},"author":0,"featured_media":11478,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[],"class_list":["post-11477","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-windows-11"],"_links":{"self":[{"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/11477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/comments?post=11477"}],"version-history":[{"count":0,"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/posts\/11477\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media\/11478"}],"wp:attachment":[{"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/media?parent=11477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/categories?post=11477"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cheapwindowsvps.com\/blog\/wp-json\/wp\/v2\/tags?post=11477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}