Skip to main content

Cleaning up a Windows server before cloning

This will free up disk space, remove logs - generally clean up a server before cloning the VHD. Use with caution!

Run Disk Cleanup as Administrator​

  • Press Win + R, type cleanmgr and hit Enter.
  • Click Clean up system files.
  • Check the boxes for Windows Update Cleanup, Temporary Setup Files, and System error memory dump files.
  • Click OK and then Delete Files

Clear System and User Temp Folders​

These folders are safe to clean, though you may receive an "Access Denied" or "File in Use" error for actively running processes; just skip those files:

  • User Temp: Press Win + R, type %temp% and press Enter. Press Ctrl + A to select all, and delete the files.
  • System Temp: Press Win + R, type C:\Windows\Temp and press Enter. Delete all files here.
  • Prefetch: Press Win + R, type prefetch and press Enter. Delete the files inside.

Clear WinSXS (System Component Cleanup)​

Over time, the Windows side-by-side (WinSXS) directory bloats with old system updates. You can safely compress and purge these using an elevated Command Prompt:

  • Open Command Prompt as Administrator.
  • Run this exact command to clean superseded update packages: \${NL}dism /online /cleanup-image /spsuperseded \${NL}
  • Run this command to reset the component base (Note: this makes previous Windows updates non-removable, but frees the maximum amount of space): \${NL}dism /online /cleanup-image /startcomponentcleanup /resetbase \${NL}

Purge Shadow Copies and System Restore​

Volume Shadow Copies will prevent a VHDX from shrinking because they lock blocks at the end of the disk. Clean them completely:

\${NL}cmdvssadmin delete shadows /all /quiet \${NL}

Clear Delivery Optimization and Cache Logs​

:: Delete Delivery Optimization Cache rmdir /s /q "C:\ProgramData\Microsoft\Network\Downloader" 2>nul rmdir /s /q "C:\Windows\SoftwareDistribution\Download" 2>nul

:: Clear all Windows Event Logs (highly recommended for templates) for /F "tokens=*" %G in ('wevtutil.exe el') do (wevtutil.exe cl "%G")

Zero-Out the Empty Space (Crucial for VHDX Shrinking)​

\${NL}sdelete64.exe -z c: \${NL}

Final Step: Shrink the VHDX `(Run on the Hyper-V Host)​

Optimize and compact the disk blocks

Optimize-VHD -Path "C:\ClusterStorage\Volume1\VMs\Template2022.vhdx" -Mode Full

Resize the VHDX to its smallest possible size

Resize-VHD -Path "C:\ClusterStorage\Volume1\VMs\Template2022.vhdx" -ToMinimumSize