** If you're looking to free up disk space and reduce the size of the WinSxS directory on Windows Server 2008 R2, make sure to check out the following blog post on this topic**
A few months back, I wrote a post on how to reduce the size of the WinSxS directory and free up disk space on Windows Server 2012.
As a recap, the WinSxS directory is a large consumer of disk space on Windows Clients and Servers. Over time, the WinSxS directory steadily increases in size.
So why is this?
One cause of this is Windows Updates. To allow the ability to uninstall an update, all the previously installed versions of Windows Updates are kept in WinSxS directory even when they become superseded by an update released in future. This accounts for Windows updates taking a lot of space and it continues to grow significantly as more and more updates are installed.
So how do we cleanup the WinSxS directory? Well, with each service pack released, we also give the option of performing a cleanup that removes all previous versions of the files updated by the service pack. However, service pack 1 released well over 2 years ago, and there has not been another service pack since. Think about all those files updated by security updates and hotfixes? Up until today, we have not had the ability to cleanup these files.
But that all changes TODAY.
New Important Update that Adds Windows Update Cleanup
This morning, a new recommended update was released. It can be found here.
This update back ports some of the same functionality built into Windows 8 and later. It gives us the ability to cleanup previous versions of the files superseded by Windows updates.
Note: This is categorized as an “important” update. What does this mean for you? This means it’s not a critical security update which means it may not be automatically installed or deployed depending on your Windows Update settings, WSUS settings, or other 3rd party patch management software settings.
Windows Update Cleanup Process
So what does this process look like?
After you install the update, click on Start, Search, type Disk Cleanup, and then right-click on Disk Cleanup and run it as Administrator.
First, you are prompted to select the drive you want to clean up. This is only valid for the operating system drive. So select C:\.
Disk Cleanup scans and calculates how much space you will be able to free on that drive:
If you didn’t launch Disk Cleanup as Administrator, at this point, you’ll need to take a couple extra steps. You’ll need to click on the Clean up system files button.
At that point, Disk Cleanup will relaunch as Administrator and you’ll need to repeat the first two steps again. So save yourself a couple extra clicks and launch Disk Cleanup as Administrator to begin with.
If you scroll through the files to delete, you’ll see the new option added by this update along with the amount of space you can save. It’s called Windows Update Cleanup. Notice it is checked by default?
Note: If nothing is found for Windows Update Cleanup to perform, it will not show up as an option.
Important note: After performing this cleanup, you may not be able to uninstall any of the updates currently applied. However, if you need to rollback to a previous version, you can still manually apply the previous version of the update.
If you highlight Windows Update Cleanup, you’re presented with the following details
“Windows keeps copies of installed updates from Windows Update, even after installing newer version of updates. Windows Update cleanup deletes older versions of updates that are no longer needed and taking up space. (You might need to restart your computer).”
When you click ok, Disk Cleanup runs and cleans up the unnecessary files on your machine. You can see it running the Windows Update Cleanup as part of this below:
Disk Cleanup finishes and exits.
In my experience with testing this update, we’re not prompted to reboot. However, when you next reboot, you’ll see Windows configuring Windows updates on shutdown and Cleaning up on startup.
The cleanup does take a few minutes so don’t be alarmed if it sits here for a minute or two. The larger the cleanup, the longer it will take. But even on my machine with 1.78GB of files to cleanup, it still ran in under 5 minutes.
The amount of space you will save will vary by machine, but here’s a screen shot from another example:
How to Automate Windows Update Cleanup
I know running around and running Disk Cleanup on 10,000 clients isn’t anyone’s idea of a good time. So how do we automate this?
Well, it’s a little cumbersome, but not too bad. It does take a bit of legwork first. It requires the export of a registry, and import of that registry on the clients. We can then use cleanmgr.exe (the command line version of Disk Cleanup) to automate Windows Update cleanup.
First a little background. Under the following registry key, we have a list of all the available disk cleanup plugins for Disk Cleanup:
Here’s an example of what this registry key looks like on my Windows 7 SP1 client:
Inside each one of these plugins, there’s a value called StateFlags. This is traditionally set to a value of 0 or 1. If it’s 1, it’s automatically checked when you run Disk Cleanup the next time. Here’s what it looks like for the Update Cleanup plugin on my Windows 7 SP1 client machine:
In order to automate Disk Cleanup using cleanmgr.exe, we need to push a new StateFlags registry value to the plugins we want to automate. So how do we do this?
Open an administrative command prompt and run the following command:
11 can be any random number you choose between 0 and 65535.
It will then look just like running Disk Cleanup. It will prompt you for the drive, present you with the options just like we did before. When it’s complete, if you go back and look at these plugins in the registry, you’ll notice the ones you had checked now have a new StateFlags value that corresponds to the number you specified with sageset. As an example here’s that same plugin now:
It’s important to note that running sageset does not perform the cleanup. It simply updates the settings for that set. The cleanup isn’t performed until you run the sage run command shown later in this post.
Now that you have this set, you can export this StateFlags registry key for each of the plugins you wish to automate and import it on your other Windows 7 SP1 clients. Just make sure you export the StateFlags registry key that was newly added by the sageset command. This will be the one that has the number you chose appended to the end. In my screen shot above, that would be StateFlags0011.
Once you have this key exported, you can push these entries through Group Policy if you’d like, use a batch file, or other means. The choice is yours.
After the import, you can then kick off cleanmgr.exe /sagerun:11 which will run through the same cleanup on all your clients.
Voila! Presto! More free space.
Similar to the registry keys, you can push out a scheduled task that would then execute the previous command on your clients say once a month. However, you may not want to be this aggressive and you need to be careful about when this runs. Remember, after you perform this cleanup, you cannot remove any Windows Updates that have been applied previously. You can still uninstall the latest updates that have been applied after the cleanup though, but it may take you all the way back to the SP1 level. You also do not want this scheduled task to run right after you apply updates. Depending on your patching schedule, you may want to schedule the cleanup to occur a day or two before regularly scheduled updates are applied. By default, the option to run a task as soon as possible after a scheduled start is missed is NOT checked. Because you do not want this to run right after applying new updates, I would advise against checking this box. If it’s missed, let the cleanup slide to the next scheduled run.
Happy Windows Update Cleanup Day!
Charity “Fall is the new Spring cleaning season” Shelbourne