Skip to content

November 17, 2015


Clear SCCM Cache by Deploying a Package

A while back, I wrote a simple PowerShell script that will clear the cache on a SCCM client machine (I will post this script for your reference at the end of this blog).

I mostly used this script during a large Task Sequences (ie. during a build and capture task sequence that installs a lot of software), since the cache would fill completely and then fail the Task Sequences (it appears that SCCM will not override any items in cache during the task sequence). This never presented an issue since I always ran the script directly from the Distribution Point.

However, there is one caveat when executing this script by deploying a SCCM package. It is important that when you deploy the package to use the “Run from Distribution Point” option. If you do not do this, the script will fail. This is because it will be running from the cache that you are trying to clear.

Here is the script I use:

Example Usage to use with SCCM Program: powershell.exe -Command “.\ClearConfigMgrCache.ps1 -DeletePersistent $true”

 [bool] $DeletePersistent
$CMObject = New-Object -ComObject "UIResource.UIResourceMgr"
$CMCacheObjects = $CMObject.GetCacheInfo()
$CMCacheElements = $CMCacheObjects.GetCacheElements()
foreach ($CMCacheElement in $CMCacheElements)
    if ($DeletePersistent -eq $true)
        $CMCacheObjects.DeleteCacheElementEx($CMCacheElement.CacheElementId, $true)
5 Comments Post a comment
  1. Francois
    Nov 19 2017

    Or a solution might be to copy the script first and then run the script locally?

    • Brent
      Nov 21 2017

      That might work; I haven’t tested that scenario. If you run locally and not from distribtion point you could still run into an issue with the copy script files still being “in use” while the Clear script works (unless the SCCM program itself contains the copy and execute commands and you don’t have to have source files). If you test this, I would be interested in knowing if it is successful.

  2. David
    Nov 9 2018

    This solution works Francois ?? I’m very interested.

  3. Ken Thompson
    Nov 22 2019

    Has anyone ever been able to get this to run as a package without running it from the DP? I can’t get anything to work when run from a package and that’s what I need. I’ve tried everything – copying it in one package and then running locally from another etc. It’s strange but I’ve been searching for weeks for a way to clear the ccmcache prior to an OS deployment but I’ve found nothing but scripts that work when run locally but never when distributed and run from a package.

    • Brent
      Nov 24 2019

      I believe part of the problem running from a package locally is because it is running from ccmcache, so it can’t clear itself. That is why running from the DP works because it isn’t using the cache. If running from the DP isn’t an option, the only think I can think of to do is to run a script that creates an external process that runs after the package completes, such as by using a scheduled task to run one minute later (leveraging a process that isn’t dependent on ccmcache).


Share your thoughts, post a comment.


Note: HTML is allowed. Your email address will never be published.

Subscribe to comments