Skip to content

How to Clear Browser Caches (Chrome, Firefox, Edge) from a Batch Script

Learn how to create a Windows Batch script to safely clear Chrome, Firefox, and Edge caches while preserving bookmarks, passwords, and extensions for developers, IT admins, and sysadmins.

Clearing browser caches is a common task for developers debugging web applications, IT professionals troubleshooting display issues, or administrators reclaiming disk space on managed machines. While every browser has a built-in menu to clear data, automating it via a Batch script is faster, more consistent, and essential when dealing with multiple browsers or deploying cleanup across a fleet. This guide shows how to locate, safely target, and purge browser caches for Chrome, Firefox, and Edge.

This guide explains how to build a comprehensive browser cache cleanup script.

Understanding Browser Cache Locations

Each browser stores temporary files in specific directories within the user's AppData. The critical distinction is between cache (safe to delete, regenerated automatically) and profile data (bookmarks, passwords, extensions, must NOT be deleted unless intended).

Cache directories (safe to delete):

Browser Cache Locations What's Stored
Chrome %LocalAppData%\Google\Chrome\User Data\[Profile]\Cache\ HTTP cache
%LocalAppData%\Google\Chrome\User Data\[Profile]\Code Cache\ Compiled JavaScript
%LocalAppData%\Google\Chrome\User Data\[Profile]\GPUCache\ GPU shader cache
%LocalAppData%\Google\Chrome\User Data\ShaderCache\ Shared GPU shaders
Edge Same structure under %LocalAppData%\Microsoft\Edge\User Data\ Same as Chrome (Chromium-based)
Firefox %LocalAppData%\Mozilla\Firefox\Profiles\[random].default\cache2\ HTTP cache
%LocalAppData%\Mozilla\Firefox\Profiles\[random].default\startupCache\ Startup optimization cache

Profile data (DO NOT delete unless resetting the browser):

Data Files/Folders Impact of Deletion
Bookmarks Bookmarks, places.sqlite All bookmarks lost
Saved passwords Login Data, logins.json All saved passwords lost
Extensions Extensions/ All extensions removed
Cookies Cookies, cookies.sqlite Logged out of all websites
History History, places.sqlite Browsing history cleared

Method 1: Multi-Browser Cache Cleanup

Clears cache for all three major browsers, handling multiple profiles per browser and providing per-browser feedback.

@echo off
setlocal EnableDelayedExpansion

echo ============================================================
echo   Browser Cache Cleanup
echo ============================================================
echo.

:: =============================================
:: Step 1: Close browser processes
:: =============================================
echo [1/2] Closing browser processes...

set "BrowsersClosed=0"

:: Chrome
tasklist /fi "IMAGENAME eq chrome.exe" 2>nul | findstr /i "chrome" >nul
if not errorlevel 1 (
    echo   [INFO] Closing Chrome...
    taskkill /f /im chrome.exe /t >nul 2>&1
    set /a "BrowsersClosed+=1"
)

:: Edge
tasklist /fi "IMAGENAME eq msedge.exe" 2>nul | findstr /i "msedge" >nul
if not errorlevel 1 (
    echo   [INFO] Closing Edge...
    taskkill /f /im msedge.exe /t >nul 2>&1
    set /a "BrowsersClosed+=1"
)

:: Firefox
tasklist /fi "IMAGENAME eq firefox.exe" 2>nul | findstr /i "firefox" >nul
if not errorlevel 1 (
    echo   [INFO] Closing Firefox...
    taskkill /f /im firefox.exe /t >nul 2>&1
    set /a "BrowsersClosed+=1"
)

if !BrowsersClosed! equ 0 (
    echo   [OK] No browsers were running.
) else (
    echo   [OK] !BrowsersClosed! browser(s^) closed.
    :: Wait for processes to fully terminate and release file handles
    timeout /t 3 /nobreak >nul
)

echo.

:: =============================================
:: Step 2: Clear caches
:: =============================================
echo [2/2] Clearing browser caches...
echo.

set "TotalCleared=0"

:: ----- Google Chrome -----
set "ChromeBase=%LocalAppData%\Google\Chrome\User Data"
if exist "!ChromeBase!\" (
    echo   [Chrome]
    :: Clear cache for ALL profiles (Default, Profile 1, Profile 2, etc.)
    for /d %%p in ("!ChromeBase!\*") do (
        set "ProfileName=%%~nxp"

        :: Only process directories that look like profiles
        if exist "%%p\Cache\" (
            call :ClearDir "%%p\Cache" "!ProfileName!/Cache"
        )
        if exist "%%p\Code Cache\" (
            call :ClearDir "%%p\Code Cache" "!ProfileName!/Code Cache"
        )
        if exist "%%p\GPUCache\" (
            call :ClearDir "%%p\GPUCache" "!ProfileName!/GPUCache"
        )
        if exist "%%p\Service Worker\CacheStorage\" (
            call :ClearDir "%%p\Service Worker\CacheStorage" "!ProfileName!/ServiceWorker"
        )
    )
    :: Shared shader cache
    if exist "!ChromeBase!\ShaderCache\" (
        call :ClearDir "!ChromeBase!\ShaderCache" "ShaderCache"
    )
    echo.
) else (
    echo   [Chrome] Not installed.
    echo.
)

:: ----- Microsoft Edge -----
set "EdgeBase=%LocalAppData%\Microsoft\Edge\User Data"
if exist "!EdgeBase!\" (
    echo   [Edge]
    for /d %%p in ("!EdgeBase!\*") do (
        if exist "%%p\Cache\" (
            call :ClearDir "%%p\Cache" "%%~nxp/Cache"
        )
        if exist "%%p\Code Cache\" (
            call :ClearDir "%%p\Code Cache" "%%~nxp/Code Cache"
        )
        if exist "%%p\GPUCache\" (
            call :ClearDir "%%p\GPUCache" "%%~nxp/GPUCache"
        )
        if exist "%%p\Service Worker\CacheStorage\" (
            call :ClearDir "%%p\Service Worker\CacheStorage" "%%~nxp/ServiceWorker"
        )
    )
    if exist "!EdgeBase!\ShaderCache\" (
        call :ClearDir "!EdgeBase!\ShaderCache" "ShaderCache"
    )
    echo.
) else (
    echo   [Edge] Not installed.
    echo.
)

:: ----- Mozilla Firefox -----
set "FirefoxBase=%LocalAppData%\Mozilla\Firefox\Profiles"
if exist "!FirefoxBase!\" (
    echo   [Firefox]
    for /d %%p in ("!FirefoxBase!\*") do (
        if exist "%%p\cache2\" (
            call :ClearDir "%%p\cache2" "%%~nxp/cache2"
        )
        if exist "%%p\startupCache\" (
            call :ClearDir "%%p\startupCache" "%%~nxp/startupCache"
        )
        if exist "%%p\jumpListCache\" (
            call :ClearDir "%%p\jumpListCache" "%%~nxp/jumpListCache"
        )
    )
    echo.
) else (
    echo   [Firefox] Not installed.
    echo.
)

echo ============================================================
echo   Cache cleanup complete. %TotalCleared% cache(s^) cleared.
echo ============================================================

endlocal
exit /b 0


:: =============================================
:: Subroutine: Clear a directory's contents
:: %1 = directory path, %2 = display label
:: =============================================
:ClearDir
if not exist "%~1\" exit /b 0

:: Delete files
del /q /s /f "%~1\*.*" >nul 2>&1

:: Delete subdirectories
for /d %%d in ("%~1\*") do rd /s /q "%%d" 2>nul

echo     Cleared: %~2
set /a "TotalCleared+=1"
exit /b 0

Why all profiles are cleaned:

Chrome and Edge support multiple profiles (Default, Profile 1, Profile 2, Guest Profile, etc.). Each profile has its own independent cache. A script with no wildcard will only clear the Default profile, missing cache data from all other profiles. The wildcard loop for /d %%p in ("User Data\*") handles any number of profiles.

Why Service Worker cache is included:

Modern web applications use Service Workers to cache resources for offline use. This cache persists even after clearing the regular HTTP cache and can cause developers to see stale application content. Clearing Service Worker\CacheStorage ensures a complete reset.

Method 2: Selective Browser Cleanup

When you only need to clear one specific browser, useful for developers testing in a specific browser or IT troubleshooting a browser-specific issue.

@echo off
setlocal EnableDelayedExpansion

set "Browser=%~1"

if "%Browser%"=="" (
    echo Usage: %~nx0 ^<browser^>
    echo.
    echo Browsers: chrome, edge, firefox, all
    echo.
    echo Examples:
    echo   %~nx0 chrome
    echo   %~nx0 firefox
    echo   %~nx0 all
    endlocal
    exit /b 1
)

:: Map browser name to process and cache path
if /i "%Browser%"=="chrome" (
    set "ProcessName=chrome.exe"
    set "CacheBase=%LocalAppData%\Google\Chrome\User Data"
    set "BrowserName=Google Chrome"
) else if /i "%Browser%"=="edge" (
    set "ProcessName=msedge.exe"
    set "CacheBase=%LocalAppData%\Microsoft\Edge\User Data"
    set "BrowserName=Microsoft Edge"
) else if /i "%Browser%"=="firefox" (
    set "ProcessName=firefox.exe"
    set "CacheBase=%LocalAppData%\Mozilla\Firefox\Profiles"
    set "BrowserName=Mozilla Firefox"
) else if /i "%Browser%"=="all" (
    echo [INFO] Clearing all browsers...
    call "%~f0" chrome
    call "%~f0" edge
    call "%~f0" firefox
    endlocal
    exit /b 0
) else (
    echo [ERROR] Unknown browser: %Browser% >&2
    echo Supported: chrome, edge, firefox, all >&2
    endlocal
    exit /b 1
)

echo [ACTION] Clearing %BrowserName% cache...

:: Close the browser if running
tasklist /fi "IMAGENAME eq %ProcessName%" 2>nul | findstr /i "%ProcessName%" >nul
if not errorlevel 1 (
    echo   Closing %BrowserName%...
    taskkill /f /im %ProcessName% /t >nul 2>&1
    timeout /t 2 /nobreak >nul
)

:: Clear caches
if not exist "!CacheBase!\" (
    echo   [INFO] %BrowserName% not installed or no profile data found.
    endlocal
    exit /b 0
)

if /i "%Browser%"=="firefox" (
    :: Firefox uses random profile directory names
    for /d %%p in ("!CacheBase!\*") do (
        if exist "%%p\cache2\" del /q /s /f "%%p\cache2\*.*" >nul 2>&1
        if exist "%%p\startupCache\" del /q /s /f "%%p\startupCache\*.*" >nul 2>&1
    )
) else (
    :: Chrome/Edge (Chromium-based) structure
    for /d %%p in ("!CacheBase!\*") do (
        for %%c in (Cache "Code Cache" GPUCache) do (
            if exist "%%p\%%~c\" (
                del /q /s /f "%%p\%%~c\*.*" >nul 2>&1
                for /d %%d in ("%%p\%%~c\*") do rd /s /q "%%d" 2>nul
            )
        )
    )
)

echo   [OK] %BrowserName% cache cleared.

endlocal
exit /b 0

Usage:

clear_cache.bat chrome      :: Clear Chrome only
clear_cache.bat firefox     :: Clear Firefox only
clear_cache.bat edge        :: Clear Edge only
clear_cache.bat all         :: Clear all three

Method 3: Cache Size Report (Before Cleanup)

See how much space browser caches are consuming before deciding to clear them.

@echo off
setlocal EnableDelayedExpansion

echo [INFO] Browser cache size report:
echo --------------------------------------------------
echo.

:: Chrome
set "ChromeBase=%LocalAppData%\Google\Chrome\User Data"
if exist "!ChromeBase!\" (
    for /f "delims=" %%s in (
        'powershell -NoProfile -Command ^
            "$paths = Get-ChildItem ''!ChromeBase!'' -Directory | ForEach-Object {" ^
            "    ''Cache'', ''Code Cache'', ''GPUCache'' | ForEach-Object {" ^
            "        Join-Path $_.FullName $_" ^
            "    }" ^
            "} | Where-Object { Test-Path $_ };" ^
            "$size = ($paths | ForEach-Object { Get-ChildItem $_ -Recurse -Force -ErrorAction SilentlyContinue } |" ^
            "    Measure-Object Length -Sum).Sum;" ^
            "[math]::Round($size / 1MB, 1)"'
    ) do echo   Chrome cache:  %%s MB
) else (
    echo   Chrome:        Not installed
)

:: Edge
set "EdgeBase=%LocalAppData%\Microsoft\Edge\User Data"
if exist "!EdgeBase!\" (
    for /f "delims=" %%s in (
        'powershell -NoProfile -Command ^
            "$paths = Get-ChildItem ''!EdgeBase!'' -Directory | ForEach-Object {" ^
            "    ''Cache'', ''Code Cache'', ''GPUCache'' | ForEach-Object {" ^
            "        Join-Path $_.FullName $_" ^
            "    }" ^
            "} | Where-Object { Test-Path $_ };" ^
            "$size = ($paths | ForEach-Object { Get-ChildItem $_ -Recurse -Force -ErrorAction SilentlyContinue } |" ^
            "    Measure-Object Length -Sum).Sum;" ^
            "[math]::Round($size / 1MB, 1)"'
    ) do echo   Edge cache:    %%s MB
) else (
    echo   Edge:          Not installed
)

:: Firefox
set "FirefoxBase=%LocalAppData%\Mozilla\Firefox\Profiles"
if exist "!FirefoxBase!\" (
    for /f "delims=" %%s in (
        'powershell -NoProfile -Command ^
            "$size = (Get-ChildItem ''!FirefoxBase!'' -Recurse -Force -ErrorAction SilentlyContinue |" ^
            "    Where-Object { $_.FullName -match ''cache2|startupCache'' } |" ^
            "    Measure-Object Length -Sum).Sum;" ^
            "[math]::Round($size / 1MB, 1)"'
    ) do echo   Firefox cache: %%s MB
) else (
    echo   Firefox:       Not installed
)

echo.
echo --------------------------------------------------

endlocal
exit /b 0

Sample output:

  Chrome cache:  487.3 MB
  Edge cache:    156.8 MB
  Firefox cache: 312.5 MB

How to Avoid Common Errors

Wrong Way: Deleting the Entire Profile Directory

:: CATASTROPHIC: deletes bookmarks, passwords, extensions, history, everything
rmdir /s /q "%LocalAppData%\Google\Chrome\User Data"

This removes ALL Chrome data, not just the cache. The user loses everything.

Correct Way: Target only the Cache, Code Cache, and GPUCache subdirectories (all methods in this guide).

Wrong Way: Clearing Only the Default Profile

:: INCOMPLETE: misses Profile 1, Profile 2, etc.
del /q /s /f "%LocalAppData%\Google\Chrome\User Data\Default\Cache\*"

Chrome and Edge support multiple profiles. Each has its own cache.

Correct Way: Loop through all profile directories:

for /d %%p in ("%LocalAppData%\Google\Chrome\User Data\*") do (
    if exist "%%p\Cache\" del /q /s /f "%%p\Cache\*" >nul 2>&1
)

Problem: Files Locked Even After Killing Browser

Chromium-based browsers spawn many background processes. Even after taskkill /f /im chrome.exe /t, some processes may linger for a few seconds.

Solution: Use /t (terminate process tree) with taskkill and wait 2–3 seconds before attempting deletion:

taskkill /f /im chrome.exe /t >nul 2>&1
timeout /t 3 /nobreak >nul

Problem: Firefox Profile Directories Have Random Names

Firefox profiles are named like a1b2c3d4.default-release, with a random prefix. You cannot hardcode the path.

Solution: All methods in this guide use for /d loops to iterate through all profiles in the Firefox Profiles directory regardless of their names.

Problem: Service Workers Serve Stale Content After Cache Clear

Web applications using Service Workers cache resources independently of the browser cache. Clearing the regular cache may not affect Service Worker-cached content.

Solution: Method 1 includes Service Worker\CacheStorage deletion. For individual sites, users can also unregister Service Workers from the browser's DevTools (Application tab → Service Workers → Unregister).

Best Practices and Rules

1. Only Target Cache Directories

Cache directories contain temporary data that the browser regenerates automatically. Never delete profile-level files (bookmarks, passwords, extensions) unless you specifically intend a full browser reset.

2. Close Browsers Before Clearing

Browsers lock their cache files while running. Always kill browser processes and wait a few seconds before attempting deletion.

3. Handle All Profiles

Chrome and Edge support multiple profiles. Firefox uses randomly named profile directories. Always use wildcard/loop patterns that handle any number of profiles with any names.

4. Include Service Worker Caches for Developer Cleanup

For web development scenarios, include Service Worker\CacheStorage to prevent stale application content from persisting through regular cache clears.

5. Warn Users About Forced Browser Closure

taskkill /f closes browsers without saving. Warn users to save their work before running the cleanup, or schedule it during off-hours.

6. Combine with DNS Flush for Complete Reset

For thorough troubleshooting, flush the DNS cache after clearing browser caches:

ipconfig /flushdns >nul 2>&1

This ensures both cached files and cached DNS resolutions are cleared.

7. Report Cache Size Before Clearing

Method 3's size report helps justify the cleanup. If caches total only 50 MB, the disruption of closing browsers may not be worth the savings. If they total 2 GB, the cleanup is clearly valuable.

Conclusion

Automating browser cache cleanup targets the correct cache directories while preserving user data (bookmarks, passwords, extensions). By handling multiple browser profiles, including Service Worker caches for developer scenarios, and providing size reporting before cleanup, you create a thorough yet safe cache management tool. Whether clearing for development debugging, performance troubleshooting, or disk space reclamation, the key principles are: close browsers first, target only cache directories, and handle all profiles.

Navigation