
Cleaning the SCCM Cache the right way with PowerShell
Hundreds of low disk space alerts in SCOM after Patch Tuesday? I know the feeling…
I’ve been struggling a bit to find a good solution for the cache cleanup problem. The cache is supposed to clean itself but that’s not always the case. I have three year old cache items that te not deleted when needed. Deleting the files directly is not supported because the Client is not notified, and the SCCM SDK is quite limited. There are some scripts out there but none of them check if the cache item is actually needed before deleting it. This approach generates a lot of unnecessary network traffic.
We can do better with a PowerShell script that deletes only unused cache items. Also, it can be added to a CI and scheduled to run after Patch Tuesday. I’ve added some nifty parameters and switches that could come in handy!
Important
If you just need to clean all the cache indiscriminately this script is overkill.
You can do it with the 5 lines of code below.
## Initialize the CCM resource manager com object
[__comobject]$CCMComObject = New-Object -ComObject 'UIResource.UIResourceMgr'## Get the CacheElementIDs to delete
$CacheInfo = $CCMComObject.GetCacheInfo().GetCacheElements()## Remove cache items
ForEach ($CacheItem in $CacheInfo) {
$null = $CCMComObject.GetCacheInfo().DeleteCacheElement([string]$($CacheItem.CacheElementID))
}
Script
Script parameters
- CleanupActions
Specifies cleanup action to perform. (‘All’, ‘Applications’, ‘Packages’, ‘Updates’, ‘Orphaned’). Default is: ‘All’.
If it’s set to ‘All’ all cleaning actions will be performed. - LowDiskSpaceThreshold
Specifies the low disk space threshold percentage after which the cache is cleaned. Default is: ‘100’.
If it’s set to ‘100’ Free Space Threshold Percentage is ignored. - ReferencedThreshold
Specifies to remove cache element only if it has not been referenced in specified number of days. Default is: 0.
If it’s set to ‘0’ Last Referenced Time is ignored. - SkipSuperPeer
This switch specifies to skip cleaning if the client is a super peer (Peer Cache). Default is: $false. - RemovePersisted
This switch specifies to remove content even if it’s persisted. Default is: $false. - LoggingOptions
Specifies logging options: (‘Host’, ‘File’, ‘EventLog’, ‘None’). Default is: (‘Host’, ‘File’, ‘EventLog’). - LogName
Specifies log folder name and event log name. Default is: ‘Configuration Manager’. - LogSource
Specifies log file name and event source name. Default is: ‘Clean-CMClientCache’. - LogDebugMessages
This switch specifies to log debug messages. Default is: $false.
Notes
You need to suppress the output when using it in a CI (You have to omit ‘Host’ in the -LoggingOptions parameter value).
Pro tip: Use it as a ‘Discovery’ script, it will save some time and there are not advantages to use remediation in this case.
Screenshots






Use Github for 🐛 reporting, or 🌈 and🦄 requests
🙏 Please subscribe or clap for this article, it makes a difference! 🙏
