In my recent quest to reduce virtual server images I noticed that a huge part of the total image is taken up by the WinSXS folder. Here is a comprehensive list of DISM commands you can run to reduce that beyond what is possible by running "Disk Cleanup" from the properties of the C: drive.
Always use a command prompt that is run as Administrator (start menu, type CMD, right click on cmd.exe, Run as Administrator)!
Dism /Online /Cleanup-Image /RestoreHealth
Use this to understand how much of your WinSXS is actually shared with (actively used by) Windows, and how much is taken up by unused features / packages and other stuff:
dism.exe /Online /Cleanup-Image /AnalyzeComponentStore
Use this to get rid of the files from before a service pack was applied:
Dism /online /Cleanup-Image /SPSuperseded
And this one will make sure only the latest necessary version of each file in WinSXS is kept around:
Dism /online /Cleanup-Image /StartComponentCleanup /ResetBase
DISM.exe /Online /English /Get-Features /Format:Table > features.lst
Then you can use this short bash command line to create a .bat file to remove all files that belong to disabled features: (use your Linux, or for windows: use the Linux subsystem in Windows 10, Cygwin on earlier versions of Windows to run bash)
grep '| Disabled\s*$' CMOD-features.lst|cut -d '|' -f 1|while read; do echo "DISM.exe /Online /Disable-Feature /featurename:${REPLY// /} /Remove"; done > remove-disabled-features.bat
Then just run the created .bat file to remove all the rest of the cruft. In theory Windows will be able to reinstall these files if you turn on any of the features, for which the files were removed, but I would not go ahead with the removal until I am reasonable sure I only remove what I will not need. YMMV!
dism /online /Cleanup-Image /scanhealth
Use this to get rid of the files which were deprecated by installed Service Packs:
DISM.exe /Image:C:\test\offline /Cleanup-Image /spsuperseded /hidesp
You can get the list of packages, and seems safe to remove the ones that are not "Installed" but "Staged" or "Superseded"
dism /online /Format:Table /Get-Packages > packages.lst
Then remove them, for example:
dism /online /remove-package /packagename:Microsoft-Windows-CodecPack-Basic-Package~31bf3856ad364e35~amd64~~6.1.7601.17514
You can also list the installed features both enabled and disabled:
dism /online /get-features >features.lst
But so far I have found no way on the older Windows versions to remove the files from WinSXS that belong to disabled features...
Always use a command prompt that is run as Administrator (start menu, type CMD, right click on cmd.exe, Run as Administrator)!
for Windows 8.x, 10 and Windows Server 2012 (R2)
It's always a good idea and can avoid a lot of strange error messages to make sure the Deployment Image is in a healthy condition:Dism /Online /Cleanup-Image /RestoreHealth
Use this to understand how much of your WinSXS is actually shared with (actively used by) Windows, and how much is taken up by unused features / packages and other stuff:
dism.exe /Online /Cleanup-Image /AnalyzeComponentStore
Use this to get rid of the files from before a service pack was applied:
Dism /online /Cleanup-Image /SPSuperseded
And this one will make sure only the latest necessary version of each file in WinSXS is kept around:
Dism /online /Cleanup-Image /StartComponentCleanup /ResetBase
More aggressive cleansing
Should you want to get rid of all the unused features as well, you can do this:DISM.exe /Online /English /Get-Features /Format:Table > features.lst
Then you can use this short bash command line to create a .bat file to remove all files that belong to disabled features: (use your Linux, or for windows: use the Linux subsystem in Windows 10, Cygwin on earlier versions of Windows to run bash)
grep '| Disabled\s*$' CMOD-features.lst|cut -d '|' -f 1|while read; do echo "DISM.exe /Online /Disable-Feature /featurename:${REPLY// /} /Remove"; done > remove-disabled-features.bat
Then just run the created .bat file to remove all the rest of the cruft. In theory Windows will be able to reinstall these files if you turn on any of the features, for which the files were removed, but I would not go ahead with the removal until I am reasonable sure I only remove what I will not need. YMMV!
For Windows 7 and Windows Server 2008 R2
It's always a good idea and can avoid a lot of strange error messages to make sure the Deployment Image is in a healthy condition:dism /online /Cleanup-Image /scanhealth
Use this to get rid of the files which were deprecated by installed Service Packs:
DISM.exe /Image:C:\test\offline /Cleanup-Image /spsuperseded /hidesp
You can get the list of packages, and seems safe to remove the ones that are not "Installed" but "Staged" or "Superseded"
dism /online /Format:Table /Get-Packages > packages.lst
Then remove them, for example:
dism /online /remove-package /packagename:Microsoft-Windows-CodecPack-Basic-Package~31bf3856ad364e35~amd64~~6.1.7601.17514
You can also list the installed features both enabled and disabled:
dism /online /get-features >features.lst
But so far I have found no way on the older Windows versions to remove the files from WinSXS that belong to disabled features...