How To Clear Down SCCM ccmcache Folder | Remove Old Files

Jason Barrett Jason Barrett | | ConfigMgr

I have seen this issue in every SCCM environment that I have worked on, You set the maximum cache size on a client, but the client always uses more size than what the cache limit is set to?

Is this a bug with SCCM? Why does the client download and store larger files than it should?  I will cover everything you need to know below.

The Issue

The reason the C:\Windows\ccmcache folder has files and folders in it that are larger then the cache limit set in your SCCM client policy is that software updates are not restricted by this value.

You can set a cache limit of 5gb, then send a software update package that is 20gb in size and the client will download it because the software update package does not check what the cache limit is.

If you are deploying software updates via sccm check out this guide I wrote which will help you to reduce the size of the software update deployments.

I have just randomly checked one of my SCCM clients, The cache limit is set to 5gb, Below you can see the C:\Windows\ccmcache folder is using 10gb of space.

sccm cache size

The Solution

The best way to clear down the SCCM ccmcache Folder is via a powershell script which will remove any cache that is older than a certain amount that we will specify.

You can then use this script in three different ways

  1. Run the powershell script manually on the SCCM client
  2. Create a schedule task on the SCCM client to run the powershell script
  3. Run the powershell script manually from the SCCM Server

I recommend to implement the schedule task as this will then run automatically and you never need to worry about the SCCM Cache folder growing large in size again.

Run Manually On The Client

If you want to address the issue only on one client then all you need to do is log on to the client and run the below powershell script.

1
2
3
4
5
6
7
8
$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)
}

The above script will now delete any cache that is older than 60 days.  If you wish to edit the script to change from 60 days simply replace 60 with the amount of days you need.

Create A Schedule Task

In my experience this is the best solution to implement as this task will run on a schedule and you will not have an issue with the cache folder growing in size again.

Below we will create a windows task schedule and will deploy it via SCCM to all clients.

To implement a schedule task that clears down the sccm cache follow these steps.

    1. Log on to your SCCM server
    2. Create a folder where you keep your SCCM applications
    3. Create the below two files in this location
    4. Create a file called sccm_cache_cleardown.ps1
    5. Put the below code in to the powershell script
      1
      2
      3
      4
      5
      6
      7
      8
      $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)
      }
    6. Next create a file called Create_Task_Scheduler.ps1
    7. Put the below code in to the powershell script
      1
      2
      3
      4
      5
      6
      7
      8
      9
      start-sleep -Seconds 300
      $TaskName = 'SCCMCacheClearDown'
      $Action = New-ScheduledTaskAction -Execute 'C:\Windows\System32\WindowsPowerShellv1.0\powershell.exe' -Argument "-NonInteractive -NoLogo -NoProfile -File 'C:\scripts\sccm_cache_cleardown.ps1'"
      $Trigger = New-ScheduledTaskTrigger -AtStartup
      $Trigger.Delay = 'PT5M'
      $Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit (New-TimeSpan -Minutes 10)
      $Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
      Register-ScheduledTask -TaskName $TaskName -InputObject $Task -User 'NT AUTHORITY\SYSTEM'
      copy-item .\sccm_cache_cleardown.ps1 -Destination C:\Scripts
    8. Replace C:\Scripts if you wish to use another folder for storing the script on the client
    9. The files for the SCCM application will look like below
      sccm application files
    10. Now we need to create an SCCM application so we can install this on all sccm clients
    11. Open the SCCM console
    12. Go to \Software Library\Overview\Application Management\Applications
    13. Right click on applications and click create application
      create sccm application
    14. Select “Manually specify the application information”
    15. Click next
    16. Give the application a name and click next
    17. Click next
    18. Click add
    19. Select “Manually specify the application information”
    20. Click next
    21. Enter the same name here you gave the application
    22. Click next
    23. In content location enter the location of the folder we put the two powershell files in to
    24. In Installation program enter Create_Task_Scheduler.ps1
      create deployment type wizard
    25. Click next
    26. Click Add Clause
    27. Add the required rules for application detection, I like to keep is simple and detect if C:\Scripts\sccm_cache_cleardown.ps1 exists
      application detection rule
    28. Click ok
    29. Click next
    30. Installation behaviour : Select “Install for system”
    31. Logon requirement : Select “Whether or not a user is logged on”
    32. Click next
    33. Click next
    34. Click next
    35. Click next
    36. Click close
    37. Click next
    38. Click next
    39. Click close
    40. Right click on the application you just created and select distributed content
      distribute content
    41. Click next
    42. Click next
    43. Click add and select your DPs you want to distribute to
    44. Click next
    45. Click next
    46. Click close

The required SCCM application is now created, I recommend you test the application first on a few test machines before deploying it to all machines.

Run SCCM Clear Down Script In SCCM Console

By adding a script to the SCCM console this will allow us to run this script on bulk on the machines we want.

To set this up follow these steps

  1. Open the SCCM console
  2. Go to \Software Library\Overview\Scripts
  3. Click Create Script
  4. Give the script a descriptive name
  5. In timeout enter 300
  6. In script copy and paste the below code
    1
    2
    3
    4
    5
    6
    7
    8
    $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)
    }
  7. Click next
    create script
  8. Click next
  9. Click close
  10. Approve the script
  11. Go to \Assets and Compliance\Overview\Device Collections
  12. Right click on the device collection you want to run the cache clear down script on
  13. Select run script
  14. Highlight the script we created
  15. Click next
  16. Click next
  17. Click next

Frequently Asked Questions

What If Content Is Deleted That Is Needed In The Future?

If an application or software update tries to install and the content has been deleted from the clients cache it will simply re-download the required files from the SCCM server.

What Is The C:\Windows\ccmcache Folder Used For?

When SCCM installs an application or software update on a client it will download the install files in to a local folder in the location C:\Windows\ccmcache then SCCM will run the install command referencing these files.

How To Control The Size Of The C:\Windows\ccmcache folder?

You can control the size of the C:\Windows\ccmcache by setting client policies within the SCCM console, But if you push out software updates via SCCM these updates ignore what the cache folder limit is which is why we are implementing the solution above to clear this folder down on a regular basis.

Does The Above Script Work On All Operating Systems?

Yes, I have used the same script for many years. I have tested this script on, Windows Server 2008, 2012, 2016, 2019, 2022. Windows 7,8,10.

Leave a Comment