PVS: How to reduce the size of a VHDX vDisk file

book

Article ID: CTX697283

calendar_today

Updated On:

Description

Dynamically expanding VHDX files can increase in size over time and may eventually grow close to the maximum size configured for the VHDX.

Operations such as installing Windows updates, installing or removing applications, and using DISM can temporarily consume significant amounts of disk space. Although files may subsequently be removed from the filesystem, the physical VHDX file may remain significantly larger than the amount of data currently stored within the volume.

This article describes how to reclaim unused space and reduce the physical size of a dynamically expanding VHDX vDisk file.



Environment

 

"Citrix is not responsible for and does not endorse or accept any responsibility for the contents or your use of these third party Web sites. Citrix is providing these links to you only as a convenience, and the inclusion of any link does not imply endorsement by Citrix of the linked Web site. It is your responsibility to take precautions to ensure that whatever Web site you use is free of viruses or other harmful items." 



"The above mentioned sample code is provided to you as is with no representations, warranties or conditions of any kind. You may use, modify and distribute it at your own risk. CITRIX DISCLAIMS ALL WARRANTIES WHATSOEVER, EXPRESS, IMPLIED, WRITTEN, ORAL OR STATUTORY, INCLUDING WITHOUT LIMITATION WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NONINFRINGEMENT. Without limiting the generality of the foregoing, you acknowledge and agree that (a) the sample code may exhibit errors, design flaws or other problems, possibly resulting in loss of data or damage to property; (b) it may not be possible to make the sample code fully functional; and (c) Citrix may, without notice or liability to you, cease to make available the current version and/or any future versions of the sample code. In no event should the code be used to support ultra-hazardous activities, including but not limited to life support or blasting activities. NEITHER CITRIX NOR ITS AFFILIATES OR AGENTS WILL BE LIABLE, UNDER BREACH OF CONTRACT OR ANY OTHER THEORY OF LIABILITY, FOR ANY DAMAGES WHATSOEVER ARISING FROM USE OF THE SAMPLE CODE, INCLUDING WITHOUT LIMITATION DIRECT, SPECIAL, INCIDENTAL, PUNITIVE, CONSEQUENTIAL OR OTHER DAMAGES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Although the copyright in the code belongs to Citrix, any distribution of the sample code should include only your own standard copyright attribution, and not that of Citrix. You agree to indemnify and defend Citrix against any and all claims arising from your use, modification or distribution of the sample code."

 

Resolution

Important: The vDisk should not contain any versions and must not be in use by any PVS target devices while performing these steps. Back up the vDisk before proceeding.

Optimize the filesystem within the vDisk

Mount the vDisk and identify the drive letter assigned to the volume.

In the following example, the vDisk is mounted as drive H:.

Run the following commands from an elevated PowerShell prompt:

Optimize-Volume -DriveLetter H -Defrag -Verbose
Optimize-Volume -DriveLetter H -SlabConsolidate -Verbose
Optimize-Volume -DriveLetter H -ReTrim -Verbose

These commands optimize the filesystem, consolidate free space where applicable, and issue TRIM operations for unused blocks.

After the commands have completed, unmount the vDisk.

 
Compact the VHDX file

The VHDX must not be mounted or in use before performing this step.

From an elevated command prompt, run:

diskpart
select vdisk file="D:\local_vdisks\2025_TD2603.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit

The compact vdisk operation attempts to reduce the physical size of the dynamically expanding VHDX by reclaiming unused space.

The configured maximum capacity of the vDisk is not changed by this procedure.

Issue/Introduction

This article describes how to reclaim unused space and reduce the physical size of a dynamically expanding VHDX vDisk file.

Additional Information

Microsoft commands documentation links:

https://learn.microsoft.com/en-us/powershell/module/storage/optimize-volume?view=windowsserver2025-ps

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/compact-vdisk