Last updated on July 23rd, 2022 at 12:23 pm
In a recent blog post, How to Expand a Disk within a VM That Is Using a VHD File, I showed you how to expand a VHDX file in logical disk size. I did this because I had a problem with the auto-growth of my SQL Server logs. Now that this problem is fixed, I want to return the VHDX file back to its normal logical size of 128GB, and make the VHDX file as small as possible. This blog post will show you how to shrink the size of a VHD file.
I can hear some of you saying, “Why? You are using a dynamically expanding VHDX file, so why bother compacting and shrinking the file?”
Refresher
As a refresher, remember that a dynamically expanding VHDX file will start at a few MBs and grow as files are added to the virtual hard drive. If you delete a file within the virtual hard drive, the space is freed up only with the virtual hard drive and not from the physical VHDX files. To put it another way, once the VHDX file is expanded, it will never shrink without you following these steps.
Here are a few reasons why you would want to shrink the VHDX file:
· Shrinking the VHDX file to the smallest possible size will save disk space on the host.
· A small VHDX file leads to faster backups. 3GB is a lot faster to copy than 128GB or 256GB.
· If a problem like the SQL Server log auto-growth happens again, I want to know about it sooner rather than later. By having a small logical VHDX file size, this problem will be seen, hopefully, earlier.
![How to Compact and Shrink the Size of a VHD File-New Volume-TB How to Compact and Shrink the Size of a VHD File-New Volume-TB](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-New-Volume-TB_thumb.jpg?lossy=2&strip=1&webp=1)
How to Compact Size of a VHD File
To begin with, I always recommend that you never install anything on the C:\ drive. It can be a huge pain to logon to a server when the C:\ drive is out of space. This of course will depend on your GPO security settings!
In my blog post, How to Expand a Disk within a VM That Is Using a VHD File, I expanded my VHDX file to 1.2TB. In a later blog post, What Is the Problem with My SSRS Server?, I cleaned up the SSRS log file, thereby returning the server to its normal operating state. The screenshot above shows you that I cleaned up my virtual hard drive and that 1.24TB are free and only 2.2GB of space are used.
![How to Compact and Shrink the Size of a VHD File-VHDX-256GB How to Compact and Shrink the Size of a VHD File-VHDX-256GB](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-VHDX-256GB_thumb.jpg?lossy=2&strip=1&webp=1)
Now looking at the physical VHDX file, I can see that it is 256GB in size (of a possible 1.2TB), but in the first screenshot you can see it’s using a little over 2.2GB of space within the VHDX file. Using the processes below, I will first shrink the volume size to 128GB then compact the physical VHDX file to the smallest possible size.
How do you go about this process? In a nutshell, I will perform the same steps I did to expand the VHD, but in reverse. I need to start with the VM itself by cleaning-up, defragging and shrinking the volume size.
VM Clean-up Tasks
There are several things to note before getting started:
- You should make a copy of the VHDX file before starting, or have a good backup of the server.
- Within the VM make sure that the Recycle Bin is emptied.
![How to Compact and Shrink the Size of a VHD File-Recycle Bin How to Compact and Shrink the Size of a VHD File-Recycle Bin](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Recycle-Bin_thumb.jpg?lossy=2&strip=1&webp=1)
- You need to defrag the drive BEFORE shrinking the volume size and compacting the VHDX file.
- Multiple defrag passes might be needed.
- If the disk has any issues, it will NOT shrink or compact. Make sure that chkdsk is run.
Shrink the Volume Size
![How to Compact and Shrink the Size of a VHD File-Shrink Volume How to Compact and Shrink the Size of a VHD File-Shrink Volume](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Shrink-Volume_thumb.jpg?lossy=2&strip=1&webp=1)
Open Computer Management and select the Disk Management node. Next, select volume (E:), right click on it and select Shrink Volume…
![How to Compact and Shrink the Size of a VHD File-Shrink Space Querying the volune](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Shrink-Space_thumb.jpg?lossy=2&strip=1&webp=1)
Wait for Computer Management to analyze the volume.
![How to Compact and Shrink the Size of a VHD File-Shrink E Shrink E Drive](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Shrink-E_thumb.jpg?lossy=2&strip=1&webp=1)
I will select the suggested new size. Click Shrink to continue.
![How to Compact and Shrink the Size of a VHD File-Shrink 128GB How to Compact and Shrink the Size of a VHD File-Shrink 128GB](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Shrink-128GB_thumb.jpg?lossy=2&strip=1&webp=1)
Now my disk is back to 128GB in size. Time to compact the VHDX file. I’ll start by shutting down the VM.
Compact the VHDX File
These next set of steps will reduce/compact the physical size of the VHDX file.
![How to Compact and Shrink the Size of a VHD File-Compact Settings How to Compact and Shrink the Size of a VHD File-Compact Settings](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Compact-Settings_thumb.jpg?lossy=2&strip=1&webp=1)
In Hyper-V Manager, bring up the Settings page for the VM. Click on the Edit button.
![How to Compact and Shrink the Size of a VHD File-Compact Locate Disk How to Compact and Shrink the Size of a VHD File-Compact Locate Disk](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Compact-Locate-Disk_thumb.jpg?lossy=2&strip=1&webp=1)
Click Next.
![How to Compact and Shrink the Size of a VHD File-Compact Choose Action Compact Choose Action](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Compact-Choose-Action_thumb.jpg?lossy=2&strip=1&webp=1)
Select Compact and then click Next. This will reduce the phyiscal size, but not the logical size of the VHDX file.
![How to Compact and Shrink the Size of a VHD File-Compact Summary Compact Summary](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Compact-Summary_thumb.jpg?lossy=2&strip=1&webp=1)
Click Finish.
![How to Compact and Shrink the Size of a VHD File-VHDX-3GB Free space](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-VHDX-3GB_thumb.jpg?lossy=2&strip=1&webp=1)
Now if you look at the VHDX’s file size, it was reduced to the bare minimum size of 3GB. Keep in mind that your minimum size may be different.
Shrink the size of a VHD file
These next steps will shrink the logical size of the VHDX file.
![How to Compact and Shrink the Size of a VHD File-Settings How to Compact and Shrink the Size of a VHD File-Settings](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Settings_thumb.jpg?lossy=2&strip=1&webp=1)
Again, in Hyper-V Manager, bring up the Settings page for the VM. Click on the Edit button.
![How to Compact and Shrink the Size of a VHD File-Locate Disk How to Compact and Shrink the Size of a VHD File-Locate Disk](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Locate-Disk_thumb.jpg?lossy=2&strip=1&webp=1)
Click Next.
![How to Compact and Shrink the Size of a VHD File-Choose Action Choose Action Shrink](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Choose-Action_thumb.jpg?lossy=2&strip=1&webp=1)
Select Shrink and then click Next.
![How to Compact and Shrink the Size of a VHD File-Configure Disk Shrink the Size of a VHD File to 128GB](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-Configure-Disk_thumb.jpg?lossy=2&strip=1&webp=1)
Set the New size to 128GB, and then click on the Finish button.
![How to Compact and Shrink the Size of a VHD File-OK How to Compact and Shrink the Size of a VHD File-OK](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-OK_thumb.jpg?lossy=2&strip=1&webp=1)
Click OK to close the VM’s Settings window. Now start the VM.
Review Volume Size
![How to Compact and Shrink the Size of a VHD File-New Volume-GB Disk size](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-New-Volume-GB_thumb.jpg?lossy=2&strip=1&webp=1)
After logging onto the VM, review the disk size. Notice that it is back to its original size.
![How to Compact and Shrink the Size of a VHD File-VHDX-April File Properties](https://b3034685.smushcdn.com/3034685/wp-content/uploads/2017/05/How-to-Compact-and-Shrink-the-Size-of-a-VHD-File-VHDX-April_thumb.jpg?lossy=2&strip=1&webp=1)
Now look at the physical VHDX file (see screenshot above). It is now 3.03GB in size. When you compare this to the 256GB it was at the beginning of this blog post, that’s a huge size difference. This will help make my backups faster due to the smaller file size!
If you have any questions about how to shrink the size of a VHD file, please feel free to contact me @GarthMJ.