Tidy Cache – Clean Up Old CCMCache

You are probably already aware that ConfigMgr manages its client cache pretty well. I’ve run into situations where server VMs are running leaner and leaner, with much less disk space on the system drive than desired, so I built a compliance settings rule that will help keep the cache a little more tidy than we do out-of-the-box.

This script is an example to purge all content in the ccmcache folder that is older than 60 days.  Below is a simple example for creating a compliance settings rule.

Create a new Configuration Item CI, and paste the contents of the Discovery script and Remediation script into their respective locations. Create a new compliance rule, for where the count from your discovery script equals zero, as shown below.

Discovery Script:

#discover
$MinDays = 60
$UIResourceMgr = New-Object -ComObject UIResource.UIResourceMgr
$Cache = $UIResourceMgr.GetCacheInfo()
($Cache.GetCacheElements() |
where-object {[datetime]$_.LastReferenceTime -lt (get-date).adddays(-$mindays)} |
Measure-object).Count

Remediation Script:

#remediate
$MinDays = 60
$UIResourceMgr = New-Object -ComObject UIResource.UIResourceMgr
$Cache = $UIResourceMgr.GetCacheInfo()
$Cache.GetCacheElements() |
where-object {[datetime]$_.LastReferenceTime -lt (get-date).adddays(-$mindays)} |
foreach {
$Cache.DeleteCacheElement($_.CacheElementID)
}

tidycache

Deploy this script (with remediation) to your test lab group, and verify that it properly purges content that is older than 60 days in the ccmcache folder.

All Set! Test, test, and release to prod!

Greg

 

About Greg Ramsey
Greg Ramsey is a systems engineer specializing in global systems management for Dell IT. He has a B.S. in Computer Sciences and Engineering from the Ohio State University and is a Microsoft Most Valuable Professional (MVP) for Microsoft System Center Configuration Manager. Greg coauthored SMS 2003 Recipes: A Problem-Solution Approach (Apress, 2006), Microsoft System Center Configuration Manager Unleashed (Sams, 2009), Microsoft System Center 2012 Configuration Manager Unleashed (Sams, 2012), and Microsoft System Center 2012 Configuration Manager: Administration Cookbook (Packt Publishing, 2012), and Microsoft System Center 2012 Configuration Manager R2 Unleashed (Sams, 2012). Greg is cofounder of the Ohio SMS Users Group, and the Central Texas Systems Management User Group.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: