Previously I’ve covered how to increase disk space in Windows server, now we’ll be outlining how to do the opposite by shrinking the disk space for a virtual machine running Windows Server 2012 R2. Firstly we will be shrinking the file system within the operating system, and then shrinking the actual virtual disk afterwards to reclaim space.
As of Windows Server 2012 R2, Hyper-V allows us to dynamically shrink and expand the size of our virtual hard disk while the virtual machine is still running, meaning that there is no down time. Prior to 2012 R2 you would have to shut down the virtual machine to complete the process, I’ll cover where that’s done later on in case you’re using a version prior to 2012 R2.
Shrinking the available disk space on a server may allow us to use the space elsewhere, perhaps on another virtual machine. Typically most provisioning nowadays is thinly provisioned where the virtual disks will only be the size of the data stored on them rather than wastefully statically assigning the total space of the disk, so this may not be very beneficial for you. With that in mind there are still times where you may want to shrink the available disk space on the server to prevent a user using it. For example a service provider may have a customer looking to downgrade their disk space and we would want to shrink their disk back so they do not use space they are no longer paying for.
Decreasing the file system size
Our test server is running Windows Server 2012 R2 as a Hyper-V virtual machine. In this example our servers primary hard drive is 100gb with less than 9gb actually in use as shown below.
We would like to shrink it down to 50gb as it is not using this space and we don’t want the server to be capable of using this space in the future.
From Server Manager, select Tools > Computer Management.
Once the Computer Management window has loaded up, select Disk Management from the storage menu on the left hand side. You will now be able to see the disks and partitions available on the server. In this example we have just the single 100gb disk where the majority of the space has been assigned to the C:\ drive – this is what we want to lower first.
Right click the C:\ partition and select “Shrink Volume…” as shown below.
Next enter the amount of space in MB that you want to decrease by, in this example I’ve selected 51200MB so that the C:\ partition will be 50gb in total after completion.
After clicking the shrink button you will see the space become listed as unallocated.
Now that the space is listed as unallocated within the operating system, we can decrease the virtual disk through Hyper-V manager.
Decreasing the file system size
This section can only be done with the virtual machine online and running if you are using Windows Server 2012 R2 or newer. If you are using Windows Server 2012 (Non R2) or older, you will have to power off the virtual machine at this step to proceed. Your user account will also need to be a member of the Hyper-V Administrators group to complete this process. Additionally the virtual machine needs to be using the .vhdx format for online resizing support, if you have an older .vhd disk you will need to shut down.
With the virtual machine selected in Hyper-V Manager click on “Edit Disk…” in Actions menu on the right hand side.
This will open the Edit Virtual Hard Disk Wizard, click next to get started on the before you begin screen.
Next you’ll need to browse to the location of your virtual machines disk, once you’ve selected it click next to continue.
On the Choose Action screen you will be given the options to compact, expand, or shrink the virtual disk. Compacting can be used to attempt to minimize the total file size of the virtual disk and isn’t a bad option to run, however in this instance we’re after shrink, so select that and click next.
On the Shrink Virtual Hard Disk screen you will see the current size of the disk, and the minimum size that can be selected. This minimum size is based on the shrinking that has already taken place within the operating system that we did earlier, it detects the space marked as free here. In this case the minimum I can select is 51gb as my disk is using a bit over 50gb (50gb C:\, 300mb recovery partition and 99mb EFI system partition).
Review the changes that you’re about to apply and select Finish to complete the process.
If you are running an operating system older than Windows Server 2012 R2 and you had to shut down the server to complete the above steps, you can now power it back on. If you’re using Windows Server 2012 R2 you’re done and the whole process has been completed with no noticeable downtime.
Checking the changes
Back in the virtual machine check the Computer Management console, if you have left it open while performing the above process you’ll need to refresh it to get up to date information, this can be done by pressing the F5 key.
In my example you can see there is still a bit of unallocated space at the end, this is because earlier in the operating system I shrunk the disk so that the C:\ partition would be 50gb. Ideally I probably should have made the whole disk 50gb, so C:\ would have been a bit smaller, and there should then not be left over space. The space comes from only being able to shrink by 51gb as shown previously, that was the lowest I could go.
To make use of this unallocated space, you can simply right click it and select “Expand Volume…” then follow the wizard through. Had everything been done correctly there should have been no unallocated space left, so you may want to ensure when you lower the disk you do it GB chunks to avoid this.
Decreasing the space of a virtual hard disk in Windows Server 2012 R2 is quick, easy and can be done while running without shutting down. If you’re running a version of Windows Server prior to 2012 R2 you will need to shut down to decrease the virtual hard disk however. Shrinking the disk is first done within the operating system, once that space is marked as free it can then be removed through Hyper-V manager.