Cleaning up a Windows server before cloning

Created by Jeremy Burgess, Modified on Tue, 21 Jul at 5:38 PM by Jeremy Burgess

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

  1. Press Win + R, type cleanmgr and hit Enter.
  2. Click Clean up system files.
  3. Check the boxes for Windows Update Cleanup, Temporary Setup Files, and System error memory dump files.
  4. 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:
dism /online /cleanup-image /spsuperseded
  • Run this command to reset the component base (Note: this makes previous Windows updates non-removable, but frees the maximum amount of space):
dism /online /cleanup-image /startcomponentcleanup /resetbase


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:

cmdvssadmin delete shadows /all /quiet

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)

sdelete64.exe -z c:

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


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article