Local copy to make sure I don’t lose the original if the site goes down:
How to Clean up the WinSxS Directory and Free Up Disk Space on Windows Server 2008 R2 with New Update
It’s finally here! After pages and pages of comments from you requesting the ability to clean up the WinSxS directory and component store on Windows Server 2008 R2, an update is available.
As a refresher, the Windows Server 2008 R2 update is directly related to my previous blog post announcing a similar fix for Windows 7 client.
The Windows 7 version of this fix introduced an additional option to the Disk Cleanup wizard that would cleanup previous versions of Windows Update files. KB2852386 adds a Disk Cleanup option on Windows Server 2008 R2, similar to the Windows 7 update.
What does this mean for Windows Server 2008 R2? After installing this update and prior to being able to perform the cleanup, the Desktop Experience feature must be installed. Why you ask? Disk Cleanup is not installed by default on Windows Server 2008 R2. It is instead a component installed with the Desktop Experience feature.
Why was the update not included as a DISM switch like Windows Server 2012 R2?
This was evaluated, however, due to the amount of changes required and the rigorous change approval process, it was not feasible to back port the functionality this way. Knowing that it would be some time before everyone could upgrade to Windows Server 2012 R2 and based on feedback from an internal survey taken of a subset of enterprise customers, it was determined that this update would still be useful in its Disk Cleanup form, even with the Desktop Experience prerequisite. We hope you agree. However, we are aware that for some of you, the Desktop Experience requirement will be a deal breaker, but decided to release it anyway hoping it will help in some instances.
How can I get the update?
The update is available on Windows Update. It can also be manually downloaded from the Microsoft Update Catalog. The KB article listed above will also direct you to a download link in the Microsoft Download Center.
Let’s Cleanup those Old Windows Update Files!
First, let’s take a look at our starting point. Looking at my Windows 2008 R2 Server with SP1 installed, according to Windows Explorer, the size of my Windows/WinSxS directory is as follows:
The size of the WinSxS directory will vary by server. Some of you will have smaller WinSxS directories, some larger.
Installing the update is just like installing any other update. Just download and double-click on the .msu file:
Installing the update does not require Desktop Experience to be installed beforehand, but if you check your WinSxS directory again, you’ll see there has been no change to the size. This is expected as we need to run Disk Cleanup in order for this to take effect. It also does not require a reboot to install the hotfix.
But…we can’t do anything with what we just installed until we get Disk Cleanup which is installed with the Desktop Experience feature.
When installing Desktop Experience, it does require additional features. Select the button to Add Required Features and click Next and then Install:
A reboot is required to finalize the install.
Click Close and Reboot when prompted.
After we reboot, a Disk Cleanup option can be found under Start –> All Programs –> Accessories –> System Tools:
On launch, Disk Cleanup prompts for the drive you want to clean up:
After clicking Ok, a scan is performed:
Several options are provided for cleanup, including a new option for Windows Update Cleanup:
Just like the Windows 7 cleanup, mileage will vary. Also like Windows 7, the actual cleanup occurs during the next reboot. After the reboot, taking a look at the WinSxS directory, it has shrunk to the following:
My super knowledgeable scripting cohort Tom Moser wrote a PowerShell script that automates THE ENTIRE PROCESS. Can I get a cheer? Ok. So maybe it is a bit much to expect IT admins to cheer, but can I get an appreciative grunt? The script certainly beats the alternative of doing this all manually.
You can find the script on the TechNet Script Center here:
What does the script do?
In short, the script does the following:
1) Installs Desktop Experience, if not previously installed, and performs a reboot.
2) Sets the appropriate registry keys to automate the cleanup. The script will cleanup not only previous Windows Update files as well as Service Pack files.
3) The script then initiates the cleanup.
4) If Desktop Experience was not previously installed, the script uninstalls it.
5) Performs final reboot.
For more details, read below.
The script can be run from any directory on the server. It has two parameters: LogPath and a switch called NoReboot. LogPath will allow the user to specify a log location or if none is specified, by default, the script will create a log in the same directory from which the script was executed. NoReboot allows the user to suppress reboots, but will require manual reboots by an administrator.
Note: Make sure to check the log file to verify the process completed successfully and to verify there is no manual interaction required. If the script has completed successfully, the log will end with CleanMgr complete.
The script has several phases, using a registry key to keep track of progress. After initial run, it inserts itself as a scheduled task, which runs as local system. The final phase removes the task.
Depending on pending reboots, etc, we have found that this phase may generate a few reboots. Do not be concerned if the server reboots a few times.
Aside from the cleanup mechanism included with this fix, if you have applied SP1 and have not cleaned up afterwards, I’d highly recommend doing so by running the following command from an administrative command prompt:
dism /online /cleanup-image /spsuperseded
If you have installed the Desktop Experience feature and thus have the Disk Cleanup utility, you can select the following option to do the same thing:
Specifying the /spsuperceded switch or choosing to remove service pack backup files will remove the ability to uninstall the service pack. If you haven’t done it before, it is certain to free up some space.
The Origins of this Update (Hint: Windows Server 2012 R2)
I’ve mentioned a couple of times that this is a back port. What does that mean? Well, it means that this functionality is already built into a later operating system. In this case, that operating system is Windows Server 2012 R2. Not only do we have several mechanisms to automatically cleanup previous versions of Windows Update files like this update does, we even have the ability to more accurately determine the size of the component store (aka the WinSxS directory).
The command to accurately determine the size of the component store on Windows Server 2012 R2 is as follows:
Dism.exe /Online /Cleanup-Image /AnalyzeComponentStore
Running this command analyzes the component store to determine the size and whether cleanup is recommended. Notice in the screen shot that it provides you with the Windows Explorer reported size and the actual size:
Notice that the component store is much smaller than Windows Server 2008 R2 right out of the gate? This isn’t because I’ve used Features on Demand to remove roles and features. It’s because by default in Windows Server 2012 R2, we compress all unused binaries. Another win for Windows Server 2012 R2!
Looking at the breakdown of the 5.12GB. We see that Shared with Windows accounts for 3.83GB of the 5.12GB. Shared with Windows refers to the size of the files that are hardlinked between the WinSxS directory and the Windows location of the file. Because these hardlinks appear to take up space, but don’t really, we can subtract them from our component store size. Therefore, the actual size of the component store is the total of Backups and Disabled Features plus Cache and Temporary Data or 1.28GB.
But back to our cleanup.
In the above screen shot, it’s stated that component store cleanup is recommended. We can manually cleanup the component store on Windows Server 2012 R2 by running the following command:
Dism.exe /online /Cleanup-Image /StartComponentCleanup
What does this do? When this runs, Windows cleans up the previous versions of the component that was updated. In other words, it is doing exactly what our update does for Windows Server 2008 R2 SP1. It removes previous versions of the files updated by Windows Updates.
After running /StartCompomentCleanup, upon analyzing the size again, we see it is as follows:
So no notable difference really. Largely because we’ve been running this cleanup all along. This same command is run every 30 days as a scheduled task with a time limit of 1 hour.
With the scheduled task however, the task will wait at least 30 days after an updated component has been installed before uninstalling the previous versions of the component. This scheduled task can be found in Task Scheduler under the Task Scheduler Library\Microsoft\Windows\Servicing\StartComponentCleanup directory:
More information on this can be found here: http://technet.microsoft.com/en-us/library/dn251565.aspx
If you’re in all out spring cleaning mode and want to perform super deep cleanup, you can use the /resetbase command with the /startcomponentcleanup to remove all superseded versions of every component in the component store:
Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase
This removes the ability to uninstall any updates applied until this point in time.
And don’t forget the ability to completely remove any role or feature which also reduces the size. Take a look at one of my earlier blogs for more details on Features on Demand: http://blogs.technet.com/b/askpfeplat/archive/2013/02/24/how-to-reduce-the-size-of-the-winsxs-directory-and-free-up-disk-space-on-windows-server-2012-using-features-on-demand.aspx
Here’s a handy table showing when we introduced the various different cleanup and WinSxS size reductions by operating system:
|Operating System||Compress Unused WinSxS Binaries||Cleanup Previous Windows Update Files||Automatically Clean Up Previous Windows Update Files||Cleanup All Components||Features on Demand|
|Windows Server 2008 R2||With KB2852386|
|Windows Server 2012||With KB2821895||x||x||x|
|Windows Server 2012 R2||x||x||x||x||x|
Want more information on how all this works under the covers?
Check out the following series on the AskCore team blog for an in-depth look at servicing improvements on Windows Server 2012 R2:
What’s New in Windows Servicing: Part 1
What’s New in Windows Servicing: Reduction of Windows Footprint : Part 2
What’s New in Windows Servicing: Service Stack Improvements: Part 3
More on the Desktop Experience Feature
The Desktop Experience feature includes the following components and features:
* Windows Media Player
* Desktop themes
* Video for Windows (AVI support)
* Windows SideShow
* Windows Defender
* Disk Cleanup
* Sync Center
* Sound Recorder
* Character Map
* Snipping Tool
* Ink Support
Most of these are not automatically turned on with the exception of Windows Defender whose service is started after reboot. You’ll likely want to stop the service and disable it after reboot. Not all 3rd party anti-viruses conflict with Windows Defender, but there have been reports that some do.
~ Charity Shelbourne and Tom Moser, Spring cleaning servers since 1998
Update May 15th, 2014
We are aware of a method of copying in the appropriate Disk Cleanup/CleanMgr files into the appropriate location to avoid installing the Desktop Experience. If this were a tested and supported option, we certainly would have included these details in this post and definitely would have used this method to automate the cleanup. However, it was determined early on that this method would not be supported. If you decide to do this, do so at your own risk.