Clear Chrome Cache
Powershell Script

I’ve been encountering a lot of issues lately stemming from chrome cache needing cleared.

This is a powershell script you can point at a device which will delete cache, history, cookies, and then force close chrome.

You could schedule it as general weekly maintenance if you are encountering cache issues.

Keep in mind the first time a user loads up chrome again after this is run it they will have some loading issues.

It’s not the most graceful solution but if you are having authentication or addon issues it’s a good maintenance script.

It will also create a log file located at C:\logs and will tell you how many files it cleared:

Here is a link to the powershell script, or just copy and paste the code below.

Can set up automation in task scheduler or from software like Autotask or Syncro

				
					# Updated 12.13.23 -JL
# Script for clearing Chrome cache, running as system. Works with the user signed in or out
# Clears cache, browser history, and cookies of all user profiles on the device
# It will target Profile 1 and Default for all users
# WILL FORCE CLOSE CHROME
------------------------------

# Check to see if log directory exists

if (-not (Test-Path "C:\logs")) {
    New-Item -Path "C:\logs" -ItemType Directory
}

# Exclude the following files
$exclusionList = @("index", "data_0", "data_1", "data_2", "data_3", "data_4", "data_5", "data_6", "data_7", "data_8", "data_9")

# Force close any running instance of Google Chrome
Get-Process chrome -ErrorAction SilentlyContinue | Stop-Process -Force

# Get all user profiles on the machine
$userProfiles = Get-WmiObject Win32_UserProfile | Where-Object { $_.Special -eq $false }

foreach ($profile in $userProfiles) {
    $localAppDataPath = "$($profile.LocalPath)\AppData\Local"
    
    # Clear Google Chrome cache, history, and cookies for each profile if it exists
    foreach ($chromeProfile in @("Profile 1", "Default")) {
        $chromeDataPath = "$localAppDataPath\Google\Chrome\User Data\$chromeProfile"
        $chromeCacheDataPath = "$chromeDataPath\Cache\Cache_Data\*"
        $chromeHistoryPath = "$chromeDataPath\History"
        $chromeCookiesPath = "$chromeDataPath\Cookies"

        try {
            # Delete cache files
            if (Test-Path $chromeCacheDataPath) {
                $filesToDelete = Get-ChildItem $chromeCacheDataPath | Where-Object { $_.Name -notin $exclusionList }
                $count = $filesToDelete.Count
                $filesToDelete | Remove-Item -Force -Recurse
                Write-Output "$(Get-Date) - Google Chrome cache for $chromeProfile in $($profile.LocalPath): Cleared $count files, excluding specified files." | Out-File "C:\logs\chromecache.log" -Append
            }

            # Delete history and cookies
            if (Test-Path $chromeHistoryPath) {
                Remove-Item $chromeHistoryPath -Force
                Write-Output "$(Get-Date) - Google Chrome history for $chromeProfile in $($profile.LocalPath): Cleared." | Out-File "C:\logs\chromecache.log" -Append
            }
            if (Test-Path $chromeCookiesPath) {
                Remove-Item $chromeCookiesPath -Force
                Write-Output "$(Get-Date) - Google Chrome cookies for $chromeProfile in $($profile.LocalPath): Cleared." | Out-File "C:\logs\chromecache.log" -Append
            }
        } catch {
            Write-Error "Failed with an error for $chromeProfile in $($profile.LocalPath). Error: $_" | Out-File "C:\logs\chromecache.log" -Append
        }
    }
}