Skip to content

Instantly share code, notes, and snippets.

@sefatanam
Last active October 23, 2021 15:28
Show Gist options
  • Save sefatanam/4201cdd11af65aa59ccde8a5e0ecb386 to your computer and use it in GitHub Desktop.
Save sefatanam/4201cdd11af65aa59ccde8a5e0ecb386 to your computer and use it in GitHub Desktop.
Reclaim Disk Space by Compacting Your Docker Desktop

Reclaim Disk Space by Compacting Your Docker Desktop

After using docker desktop for quite long time it take disk space up to 50 GB or more for it's cache objects, images & junkey files. To Reclaim disk space there are some few steps to follow :

  • Step 1

# Before cleaning docker cache object see your existing docker system details

>> docker system info

# Now clean all caches it takes around 30 sec or less

>> docker system prune --all

# then you will got 

WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache

Are you sure you want to continue? [y/N] y

# Give permission & wait.
# This Operation will be end with this line in my case I got 7 GB after using one week of docker desktop.

>>  Total reclaimed space: 7.027GB  ( I got 7GB )

  • Step 2


# Now You need to stop WSL for compact it.

Firstly Open Powershell as Administrator

# Go to wsl's docker folder in AppData by

>> cd C:\Users\YOUR_USERNAME\AppData\Local\Docker\wsl\data

# Inside it run this command

>> optimize-vhd -Path .\ext4.vhdx -Mode full [IF THIS IS FAILED TRY BELLOW'S COMMAND]

>> Optimize-VHD -Path .\ext4.vhdx -Mode Full

# It will take around 15/20 sec . In my pc it claim more than 3GB.

So after doing these steps 'Restart Docker'. That's all. Enjoy.

@sefatanam
Copy link
Author

If it help, don't forget to give start 😎

@sefatanam
Copy link
Author

sefatanam commented Aug 31, 2021

If you get this error

optimize-vhd : Compacting the virtual disk 'C:\Users\sefat\AppData\Local\Docker\wsl\data\ext4.vhdx' failed.
The operation cannot be performed while the object is in use.
At line:1 char:1
+ optimize-vhd -Path .\ext4.vhdx -Mode full
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceBusy: (:) [Optimize-VHD], VirtualizationException
    + FullyQualifiedErrorId : ObjectInUse,Microsoft.Vhd.PowerShell.Cmdlets.OptimizeVhd

then just run this

optimize-vhd -Path .\ext4.vhdx # after , also run this command bellow
optimize-vhd -Path .\ext4.vhdx -Mode full

  • If Your vhdx size near about 3 GB than its oky , not need to clean it.

@sefatanam
Copy link
Author

Repo that can help you to come out general errors microsoft/WSL/issues

@sefatanam
Copy link
Author

optimize-vhd : Failed to compact the virtual disk.
The system failed to compact 'C:\Users\theanam\AppData\Local\Docker\wsl\data\ext4.vhdx'.
Failed to compact the virtual disk.
The system failed to compact 'C:\Users\theanam\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 .\ext4.vhdx
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceBusy: (:) [Optimize-VHD], VirtualizationException
    + FullyQualifiedErrorId : ObjectInUse,Microsoft.Vhd.PowerShell.Cmdlets.OptimizeVhd

if you got this,

then stop wsl then re-run again via,

 wsl --shutdown
 optimize-vhd -Path .\ext4.vhdx -Mode full

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment