Download SCCM Client Cache Clean Up Script

Let us learn about Download SCCM Client Cache Clean Up Script. Do you have problems with the SCCM Client cache? You can know and download an SCCM Cache Clean Up Script in this post.

I will explain how to delete the specific package from 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 one is to delete the folder with Specific Package ID /Content ID, and the second one is to delete the folder beyond a specific size. 

The full script has been provided in the below link.

Patch My PC

Update – There is a new updated script available from Matt Larkin. More details are in the below section of the post.

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.

CAS.log – The Content Access service. Maintains the local package cache on the client.

We can see the package/content information from the CAS.log and the folder name that holds the packages and application files.

Adaptiva
cac

$string=Read-CMLogfile -path $caslogpath

Read the Cas.log and look for the content id where the same log line has the location of the package folder.

$lookforcontent1=” — Cache content id:$contentId”+”*”

$ccmsetup_val= $string.LogText -match $lookforcontent1

$LocationPath=$ccmsetup_val.split()[-1]

$deleteFolder= $LocationPath.substring(9)

Script3
Download SCCM Client Cache Clean Up Script 1

From CAS.log

Screenshot with content id as input.

SCCM Cache Clean Up Script
Download SCCM Client Cache Clean Up Script 2

screenshot with PackageID as input.

SCCM Cache Clean Up Script
Download SCCM Client Cache Clean Up Script 3

SCCM Cache Clean Up Script – Folders Larger than a Specific Size

This SCCM Cache Clean Up script will remove any folder which is larger than the size of 12 MB.

[int]$sizeMb = “{0:N2}” -f ($subFolderItems.sum / 1MB)

if ($sizeMb -ge $sizeMax) {

Get-ChildItem $i.FullName | Remove-Item -Force -Recurse -Verbose }

Download 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 that is less 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 client tries to download the content every four hours until it has 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 choose the option to 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 cleanup 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))
}

10 thoughts on “Download SCCM Client Cache Clean Up Script”

  1. 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)
    }

    Reply
    • 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)
      }

      Reply
  2. 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)
    }

    Reply
    • 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.

      Reply
  3. 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.

    Reply
  4. 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.

    Reply
  5. ##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

    Reply
  6. 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”
    }

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.