Skip to content

Instantly share code, notes, and snippets.

@vyach-vasiliev
Last active June 27, 2025 07:34
Show Gist options
  • Save vyach-vasiliev/9079fe015258fa8905dfb3764699e4b9 to your computer and use it in GitHub Desktop.
Save vyach-vasiliev/9079fe015258fa8905dfb3764699e4b9 to your computer and use it in GitHub Desktop.
Compact VDisk after Docker operations

Resolving Docker Hard Disk Image File Size Issues on Windows

Is Your Docker Hard Disk Image File Too Large?

Is Docker Image Still Taking Up Space After Deletion?

If you're a Windows OS user, you may have encountered the problem of Docker images consuming excessive disk space even after they've been deleted. This long-standing issue can significantly affect system performance and storage management.

Common Problem for Windows OS Users

Many Windows users have faced the challenge of Docker image files growing unexpectedly large. Even after removing images, the disk space is not always reclaimed efficiently, leading to frustration and reduced available storage.

Optimal Solution: Using Microsoft's DISKPART Utility

Fortunately, there is a reliable method to manage this issue: the DISKPART utility from Microsoft. While it might not be the most intuitive solution, it is currently the most effective way to regain lost disk space and keep your system running smoothly.

Steps to Use DISKPART:

Example see in compact-vdisk.sh

  1. Ensure WSL is Shutdown

    • Before proceeding, make sure the WSL (Windows Subsystem for Linux) is shutdown. For instance, use the command wsl --shutdown to shutdown the Windows Subsystem for Linux.
  2. Open Command Prompt as Administrator

    • Right-click on the Start menu and select "Command Prompt (Admin)" or "Windows PowerShell (Admin)".
  3. Launch DISKPART

    • Type diskpart and press Enter.
  4. Select the Problem Disk

    • Identify the disk where Docker images are stored and type select vdisk file="<path_to_vhdx_disk_in_your_system>" (replace X with the appropriate disk number).
  5. Compact the Disk and Reclaim Space

    • Use the compact vdisk command. This command will compact volume on the selected disk.
  6. At the end

    • Just type detach vdisk and press Enter. This command will compact volume on the selected disk.
    • And exit the DISKPART using the exit command.
    • Congrats!

Additional Tips:

  • Regular Maintenance: Regularly monitor and manage Docker images to prevent unnecessary disk space usage.
  • Automate Cleanup: Consider setting up automated scripts to clean up unused Docker resources periodically.
  • Explore Alternatives: Investigate other disk management tools and Docker settings that might offer more intuitive solutions in the future.

Conclusion

Dealing with large Docker image files on Windows can be a hassle, but with the right tools and techniques, you can efficiently manage your disk space. The DISKPART utility, although not perfect, provides a practical solution to this common problem.

: script for Windows 10 and above
wsl --shutdown
diskpart
:: opening window Diskpart
:: compact my Docker disk
select vdisk file="C:\Users\Alex\AppData\Local\Docker\wsl\data\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
:: compact my WSL disk
select vdisk file="D:\WSL\Ubuntu-22.04\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment