Let’s learn about Downloading the SCCM Client Cache Clean Up Script. Do you have problems with the SCCM Client cache? This post provides information and a link to download the SCCM Cache Clean Up Script.
I will explain how to delete the specific package from the SCCM Cache by providing the PackageID /Content-ID and Delete folder, which exceeds a particular size.
These are two scripts that can be used for different purposes. The first deletes the folder with the Specific Package ID /Content ID, and the second deletes the folder beyond a specific size.
The full script is provided in the link below.
Update: Matt Larkin has a new script available. The section of the post below provides more details.
Table of Contents
SCCM Cache Clean UP with Specific Package ID or Content ID
Recently I came across a specific requirement to remove the content from SCCM Cache based on the Package ID. Delete the SCCM cache folder with the script. You have to provide the package ID or content ID of the folder you want to delete.
- SCCM Persist Content In The Client Cache Options | ConfigMgr | Endpoint Manager
- SCCM Client Troubleshooting Advanced Level With Real-World Experience By Deepak – Device Management Blog
CAS.log – The Content Access service. Maintains the local package cache on the client.
The package/content information and the folder name for the packages and application files can be seen in the CAS.log.
$string=Read-CMLogfile -path $caslogpath
Read the Cas.log and look for the content ID where the same log line has the package folder’s location.
- $lookforcontent1=” — Cache content id:$contentId”+”*”
- $ccmsetup_val= $string.LogText -match $lookforcontent1
- $LocationPath=$ccmsetup_val.split()[-1]
- $deleteFolder= $LocationPath.substring(9)
From CAS.log
Screenshot with content ID as input.
screenshot with PackageID as input.
SCCM Cache Clean Up Script – Folders Larger than a Specific Size
This SCCM Cache Clean Up script will remove any folder that is larger than 12 MB.
- [int]$sizeMb = “{0:N2}” -f ($subFolderItems.sum / 1MB)
- if ($sizeMb -ge $sizeMax) {
- Get-ChildItem $i.FullName | Remove-Item -Force -Recurse -Verbose }
Download the SCCM Cache Clean Up Script
The SCCM client cache on Windows computers stores temporary files used to install applications and programs.
If the client attempts to download a package smaller than the size of the cache but the cache is full, all required deployments keep retrying until the cache space is available, the download times out, or the retry count reaches its limit.
The client generates a status message 10050 for insufficient cache size. The client tries to download the content every four hours until it has been tried 18 times.
Cached content is not automatically deleted but remains in the cache for at least one day after the client uses that content. If you configure the package properties with the option to persist content in the client cache, the client does not automatically delete the package content from the cache.
If the cache space is used by packages downloaded within the last 24 hours and the client must download new packages, you can either increase the cache size or delete persisted cache content. This script will delete the CCM folder, which is beyond a specific size.
New Script available from GitHub
There is a new script available to clean up SCCM cache files from the client side, and you can refer to that from GitHub by Matt Larkin.
## 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))
}
We are on WhatsApp. To get the latest step-by-step guides and news updates, Join our Channel. Click here –HTMD WhatsApp.
Author
Regin is an experienced Windows SCCM and PowerShell Admin with 6.5 years of experience. He is an expert in SCCM and PowerShell. He already published several PowerShell scripts for Tech Community.
In my opinion it is better to peform the clean-up using the CM client
Example to clean all
#Delete all elements in Configuration Manager client cache
$CMObject = New-Object -ComObject “UIResource.UIResourceMgr”
$CMCacheObjects = $CMObject.GetCacheInfo()
$CMCacheElements = $CMCacheObjects.GetCacheElements()
Foreach ($CacheElement in $CMCacheElements)
{
$CMCacheObjects.DeleteCacheElementEx($CacheElement.CacheElementID)
}
Exactly, furthermore each cache element has a contentsize property. To steal from the example above, you could filter on that property:
$sizeMB = 30
$CMObject = New-Object -com “UIResource.UIResourceMgr”
$cacheInfo = $CMObject.GetCacheInfo()
$objects = $cacheinfo.GetCacheElements()
$objects | Where-Object {$_.ContentSize/1024 -gt $sizeMB} | ForEach-Object {
$cacheInfo.DeleteCacheElement($_.CacheElementId)
}
Also, to remove by ContentID:
$contentId=’2a77afe3-1e82-48da-b817-2f69e2c8218c’
$cacheFile = Get-CimInstance -Namespace root\ccm\SoftMgmtAgent -ClassName cacheinfoex -Filter “ContentId = ‘$ContentID'”
$CMObject = New-Object -com “UIResource.UIResourceMgr”
$cacheInfo = $CMObject.GetCacheInfo()
$objects = $cacheinfo.GetCacheElements()
$objects | Where-Object { $PSItem.CacheElementID -match $cacheFile.CacheId } | ForEach-Object {
$cacheInfo.DeleteCacheElement($PSItem.CacheElementId)
}
I really appreciate your comments/suggestion
Basic idea is to create script from the log and these logs has many vital information Also I didn’t find any related blog for string parsing with same activity so thought of writing one. I believe this is good start to parse the logs for future Automation.
HI all;
Thanks very much for this valuable information, what about the remediation script?
Script will remove the Cache folder of that package based on the input(Package ID or Content ID) provided
This Post really should be removed. Deleting CCMCache Items from the file system is not supported and leaves orphaned content in WMI. The Client still thinks the Content is on the system, then when it tries to call it, it’s gone, throwing errors. You need to use the methods as described in the comments. Script should also be removed from TechNet.
no scripts are working fine, they are deleting all files in ccmcache folder.
Anoop, please send me the script that you have shown in the screenshots.
##remediationscript
$date = Get-Date -Format ddMMyyyy
$Log = “CCM Cache cleanup-$date.log”
New-Item -Path “C:\temp” -Name $Log
$Logfilename = “C:\temp\$log”
$MinDays = 0
Add-content $Logfilename -value “Current Minimum days of last write date = $MinDays”
$OldCacheitems = Get-ChildItem -Path “C:\Windows\ccmcache” | Select-Object *|
where-object {[datetime]$_.LastWriteTime -lt (get-date).adddays(-$mindays)}
Add-content $Logfilename -value “Number of files in the specified criteria $OldCacheitems.Count”
foreach($item in $OldCacheitems.FullName)
{
Add-content $Logfilename -value “Deleting $item”
Remove-Item $item -Recurse -Force
Add-content $Logfilename -value “Successfully Deleted $item”
}
#Discovery script
$MinDays = 0
$OldCacheitems = Get-ChildItem -Path “C:\Windows\ccmcache” | Select-Object *|
where-object {[datetime]$_.LastWriteTime -lt (get-date).adddays(-$mindays)}
write-host $OldCacheitems.Count
CCMCache cleanup Activity Powershell script
#Discovery Script
$MinDays = 0
$OldCacheitems = Get-ChildItem -Path “C:\Windows\ccmcache” | Select-Object *|
where-object {[datetime]$_.LastWriteTime -lt (get-date).adddays(-$mindays)}
write-host $OldCacheitems.Count
##Remediation Script
$date = Get-Date -Format ddMMyyyy
$Log = “CCM Cache cleanup-$date.log”
New-Item -Path “C:\temp” -Name $Log
$Logfilename = “C:\temp\$log”
$MinDays = 0
Add-content $Logfilename -value “Current Minimum days of last write date = $MinDays”
$OldCacheitems = Get-ChildItem -Path “C:\Windows\ccmcache” | Select-Object *|
where-object {[datetime]$_.LastWriteTime -lt (get-date).adddays(-$mindays)}
Add-content $Logfilename -value “Number of files in the specified criteria $OldCacheitems.Count”
foreach($item in $OldCacheitems.FullName)
{
Add-content $Logfilename -value “Deleting $item”
Remove-Item $item -Recurse -Force
Add-content $Logfilename -value “Successfully Deleted $item”
}