There are many ways to optimize the VHD file but here is the one which is tested and succeed after almost 2 days after searching over the google.
First you need to know, why you need to optimize the VHD file?
We had one of our client who had setup Ubuntu and they installed Oracle Virtual Box to host the site but after couple of months what they figured is their dynamic disk is keep increasing in size and when they delete the content it still not syncing so they got worried and it uses the space on the Ubuntu,,, [Main Machine]
Example, they had 500 GB Hard disk and given 400 to VHD [Dynamic Disk] now their disk is 432GB and where they have saved the VHD into Ubuntu’s Document folder which uses 432 GB space so it is keep giving error for disk and of course it will, so they need to find a way to optimize the VHD.
Now, Let’s start with the easiest process:
Step 1: Run CMD as Administrator
note: We have only tested with Windows machine so we are taking the example with Windows.
Step 2: Give command
Step 3: Select the Vdisk file using following commands. [Assuming your VHD file is in E-Drive and in the directory called vhd
select vdisk file=e:\vhd\your.vhd
Step 4 Now you enter following code to attach Vhd file
attach vdisk readonly
Step 5 Now its the time to compacting the Vdisk or VHD file [It may take some time and it will show percentage counter
Step 6 Now its the time to detaching the disk