How to Clear the Thumbnail Cache in Batch Script
Windows generates and stores small preview images of your files, thumbnails, to speed up browsing in File Explorer. These previews are stored in database files within your user profile. Over time, this cache can become corrupted, leading to wrong thumbnails, missing icons, stale previews, or slow folder loading. Clearing the thumbnail cache forces Windows to regenerate fresh previews from the actual files.
This guide explains how to safely clear the thumbnail and icon caches.
Understanding the Cache Files
| File Pattern | Location | Purpose |
|---|---|---|
thumbcache_*.db | %LocalAppData%\Microsoft\Windows\Explorer\ | Thumbnail preview databases (multiple sizes) |
iconcache_*.db | %LocalAppData%\Microsoft\Windows\Explorer\ | Application icon cache databases |
IconCache.db | %LocalAppData%\ | Legacy icon cache (older Windows) |
These files are locked by explorer.exe while the Windows shell is running. You cannot delete them while Explorer is active, so the script must temporarily terminate Explorer, delete the files, and restart the shell.
Thumbnail and icon cache files are in your user profile (%LocalAppData%). Clearing them does not require administrator privileges, the script operates entirely within the current user's data.
Why thumbnails become corrupted:
- File content changed but the cached thumbnail wasn't updated
- Windows Update modified the thumbnail database format
- Disk errors corrupted the database files
- Rapid file operations (bulk rename, move, delete) left stale entries
- Third-party image editors modified files without triggering cache invalidation
Method 1: Clear Thumbnail Cache with Safety Measures
This method terminates Explorer, waits for file handles to release, deletes all thumbnail cache databases, verifies deletion, and restarts the shell, with recovery if anything goes wrong.
@echo off
setlocal
set "CacheDir=%LocalAppData%\Microsoft\Windows\Explorer"
echo ============================================================
echo Thumbnail Cache Reset Utility
echo ============================================================
echo.
:: Verify the cache directory exists
if not exist "%CacheDir%\" (
echo [ERROR] Cache directory not found: %CacheDir% >&2
echo This may indicate a non-standard Windows profile configuration. >&2
endlocal
exit /b 1
)
:: Count current cache files and size
set "FileCount=0"
set "TotalSize=0"
for %%f in ("%CacheDir%\thumbcache_*.db") do (
set /a "FileCount+=1"
)
if %FileCount% equ 0 (
echo [INFO] No thumbnail cache files found. Cache is already clean.
endlocal
exit /b 0
)
echo [INFO] Found %FileCount% thumbnail cache file(s^) in:
echo %CacheDir%
echo.
echo [WARNING] Your taskbar and desktop will temporarily disappear.
echo This is normal, Explorer will restart automatically.
echo.
set /p "Confirm=Clear the thumbnail cache? (YES/no): "
if /i not "%Confirm%"=="YES" (
echo [INFO] Cancelled. No changes made.
endlocal
exit /b 0
)
echo.
:: =============================================
:: Step 1: Terminate Explorer
:: =============================================
echo [1/3] Stopping Explorer shell...
:: Save the current working directory (taskkill changes it sometimes)
set "OrigDir=%CD%"
taskkill /f /im explorer.exe >nul 2>&1
:: Wait for Explorer to fully terminate and release file handles
echo [INFO] Waiting for file handles to release...
timeout /t 3 /nobreak >nul
:: Verify Explorer is stopped
tasklist /fi "IMAGENAME eq explorer.exe" 2>nul | findstr /i "explorer.exe" >nul
if not errorlevel 1 (
echo [WARNING] Explorer is still running. Retrying... >&2
taskkill /f /im explorer.exe >nul 2>&1
timeout /t 3 /nobreak >nul
)
:: =============================================
:: Step 2: Delete cache files
:: =============================================
echo [2/3] Deleting thumbnail cache files...
set "Deleted=0"
set "Failed=0"
for %%f in ("%CacheDir%\thumbcache_*.db") do (
del /f /q "%%f" 2>nul
if not exist "%%f" (
set /a "Deleted+=1"
) else (
set /a "Failed+=1"
echo [FAIL] Could not delete: %%~nxf >&2
)
)
echo Deleted: %Deleted% Failed: %Failed%
:: =============================================
:: Step 3: Restart Explorer
:: =============================================
echo [3/3] Restarting Explorer shell...
cd /d "%OrigDir%"
start "" explorer.exe
:: Wait for Explorer to start
timeout /t 2 /nobreak >nul
:: Verify Explorer restarted
tasklist /fi "IMAGENAME eq explorer.exe" 2>nul | findstr /i "explorer.exe" >nul
if errorlevel 1 (
echo [WARNING] Explorer may not have restarted. Trying again... >&2
start "" explorer.exe
)
echo.
if %Failed% equ 0 (
echo [OK] Thumbnail cache cleared successfully.
echo Windows will regenerate thumbnails as you browse folders.
) else (
echo [WARNING] %Failed% file(s^) could not be deleted. >&2
echo These may be locked by another application (OneDrive, cloud sync, etc.^). >&2
echo Try closing those applications and running the script again. >&2
)
echo.
echo ============================================================
endlocal
exit /b 0
Why the 3-second wait after killing Explorer:
Even after taskkill /f, the process takes a moment to fully terminate and release its file handles on the .db files. Without a delay, del attempts to delete files that are still locked, and the deletion silently fails. Three seconds is sufficient for most systems; heavily loaded systems may need longer.
Why start "" explorer.exe:
The empty quotes ("") before explorer.exe are important. Without them, start explorer.exe may interpret explorer.exe as the window title rather than the command, opening a File Explorer window instead of restarting the shell.
When taskkill /f /im explorer.exe runs, the taskbar, desktop icons, and Start menu disappear. This is expected, Explorer IS the Windows shell. The script restarts it in Step 3. If the script crashes between Steps 1 and 3, press Ctrl+Shift+Esc to open Task Manager, click File → Run new task, type explorer.exe, and click OK.
Method 2: Clear Both Thumbnail AND Icon Caches
Thumbnail issues often coincide with icon cache corruption. This method clears both caches for a complete visual reset.
@echo off
setlocal
set "CacheDir=%LocalAppData%\Microsoft\Windows\Explorer"
set "LegacyIconCache=%LocalAppData%\IconCache.db"
echo [INFO] Clearing thumbnail AND icon caches...
echo [WARNING] Taskbar and desktop will temporarily disappear.
echo.
set /p "Confirm=Proceed? (YES/no): "
if /i not "%Confirm%"=="YES" (
echo [INFO] Cancelled.
endlocal
exit /b 0
)
:: Stop Explorer
taskkill /f /im explorer.exe >nul 2>&1
timeout /t 3 /nobreak >nul
:: Delete thumbnail caches
set "Count=0"
for %%f in ("%CacheDir%\thumbcache_*.db") do (
del /f /q "%%f" 2>nul
set /a "Count+=1"
)
echo [OK] Deleted %Count% thumbnail cache file(s).
:: Delete icon caches (modern)
set "IconCount=0"
for %%f in ("%CacheDir%\iconcache_*.db") do (
del /f /q "%%f" 2>nul
set /a "IconCount+=1"
)
echo [OK] Deleted %IconCount% icon cache file(s).
:: Delete legacy icon cache
if exist "%LegacyIconCache%" (
del /f /q "%LegacyIconCache%" 2>nul
echo [OK] Deleted legacy IconCache.db.
)
:: Restart Explorer
start "" explorer.exe
echo.
echo [OK] All caches cleared. Icons and thumbnails will regenerate.
echo The first folder browse after clearing will be slower than usual
echo as Windows recreates the thumbnail database.
endlocal
exit /b 0
When to clear the icon cache:
- Application icons show as generic white squares
- Taskbar pinned icons show incorrect images
- Desktop shortcuts display the wrong icon
- After installing/uninstalling software that uses custom file type icons
First-browse slowdown:
After clearing the cache, the first time you open a folder with many images or documents, Explorer must regenerate every thumbnail. This makes the first browse noticeably slower (5–30 seconds for large folders). Subsequent visits use the freshly generated cache and are fast again.
Method 3: Non-Disruptive Cleanup (No Explorer Restart)
For situations where you cannot afford the desktop disruption (e.g., during a presentation, with critical applications open), this method schedules the cleanup for the next login or reboot.
@echo off
setlocal
set "CacheDir=%LocalAppData%\Microsoft\Windows\Explorer"
set "CleanupScript=%TEMP%\clear_thumbcache_on_login.bat"
echo [INFO] Scheduling thumbnail cache cleanup for next login...
echo [INFO] No disruption to your current session.
echo.
:: Create a self-deleting cleanup script
(
echo @echo off
echo cd /d "%CacheDir%"
echo del /f /q thumbcache_*.db 2^>nul
echo del /f /q iconcache_*.db 2^>nul
echo del /f /q "%LocalAppData%\IconCache.db" 2^>nul
echo del "%%~f0"
) > "%CleanupScript%"
:: Add to Run registry key (runs once at next login)
reg add "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" /v "ClearThumbCache" /t REG_SZ /d "\"%CleanupScript%\"" /f >nul 2>&1
if not errorlevel 1 (
echo [OK] Cleanup scheduled for next login.
echo The thumbnail cache will be cleared automatically when you
echo next sign in. No action is needed now.
echo.
echo The cleanup script runs before Explorer fully starts, so it
echo can delete the cache files before they are locked.
) else (
echo [ERROR] Could not schedule cleanup. >&2
del "%CleanupScript%" 2>nul
)
endlocal
exit /b 0
How RunOnce works:
The HKCU\...\RunOnce registry key contains commands that execute once at login, then automatically remove themselves. The cleanup script runs during the login sequence before Explorer fully initializes, when the thumbnail cache files are not yet locked. The script also deletes itself after running.
| Situation | Method |
|---|---|
| Quick fix for thumbnail issues | Method 1 (immediate, briefly kills Explorer) |
| Fixing both icon and thumbnail problems | Method 2 (clears both caches) |
| During a presentation or active work | Method 3 (schedules for next login) |
| Automated maintenance script | Method 3 (non-disruptive) |
Method 4: Using Disk Cleanup (Built-in Alternative)
Windows Disk Cleanup includes a "Thumbnails" category. This avoids killing Explorer entirely.
@echo off
setlocal
echo [INFO] Clearing thumbnails via Disk Cleanup...
:: Configure Disk Cleanup to clean only thumbnails
set "RegKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache"
reg add "%RegKey%" /v StateFlags0200 /t REG_DWORD /d 2 /f >nul 2>&1
:: Run the cleanup
cleanmgr /sagerun:200
echo [OK] Disk Cleanup launched for thumbnail clearing.
echo Note: This does not clear the icon cache, use Method 2 for that.
endlocal
exit /b 0
Why this doesn't always work:
cleanmgr clears the thumbnail cache through a different mechanism than direct file deletion. It may not clear all .db files, and it doesn't clear the icon cache. Use Methods 1–2 for a complete reset.
How to Avoid Common Errors
Wrong Way: Deleting Without Stopping Explorer
:: FAILS: Explorer has file locks on all thumbcache_*.db files
del /f /q %LocalAppData%\Microsoft\Windows\Explorer\thumbcache_*.db
:: Result: "The process cannot access the file because it is being used"
Explorer constantly reads the thumbnail databases. You cannot delete them while Explorer is running.
Correct Way: Kill Explorer first, delete, then restart Explorer (Methods 1 and 2).
Wrong Way: Killing Explorer Without Restarting
:: LEAVES USER WITH A BLANK SCREEN
taskkill /f /im explorer.exe
del /f /q thumbcache_*.db
:: Script ends without restarting Explorer
:: User has no taskbar, no desktop, no Start menu
If the script crashes or exits without start explorer.exe, the user is stranded with a blank screen. They must know to use Ctrl+Shift+Esc → Task Manager → File → Run new task → explorer.exe to recover.
Correct Way: Methods 1 and 2 always restart Explorer as the final step, with verification that it actually restarted.
Problem: Files Locked by OneDrive/Cloud Sync
Applications like OneDrive, Google Drive, Dropbox, and some antivirus tools maintain their own handles on thumbnail-related files. Even after killing Explorer, these files remain locked.
Solution: Close the specific application before running the cleanup:
:: Example: close OneDrive before cleanup
taskkill /f /im OneDrive.exe >nul 2>&1
Or use Method 3 (scheduled cleanup at login, before these applications start).
Problem: Thumbnails Keep Showing Old Images
If clearing the cache doesn't fix a wrong thumbnail, the issue may not be the cache:
- Browser images: Browser thumbnails are in browser-specific caches, not the Windows thumbnail cache.
- OneDrive files: Cloud-synced files may use server-side thumbnail data that persists through local cache clears.
- Embedded thumbnails: Some image formats (JPEG, TIFF) store thumbnails inside the file itself. If the embedded thumbnail differs from the image content, clearing the cache won't help, you need to update the embedded thumbnail using an image editor.
Windows 10/11's Storage Sense can automatically clear the thumbnail cache when disk space is low. If your thumbnails keep disappearing unexpectedly, check Settings → System → Storage → Storage Sense → Configure → Temporary Files. This may be clearing your cache proactively, causing Explorer to regenerate thumbnails frequently, which slows browsing.
Best Practices and Rules
1. Warn the User About Desktop Disruption
Killing Explorer removes the taskbar, desktop icons, and Start menu. Always inform the user (or provide a confirmation prompt) before executing.
2. Always Restart Explorer
The most dangerous failure mode is killing Explorer without restarting it. Structure your scripts so that start explorer.exe ALWAYS runs, even if the deletion step fails.
3. Wait After Killing Explorer
A 3-second delay after taskkill allows file handles to be fully released. Without this wait, file deletion silently fails on most systems.
4. Expect Slower First Browse After Clearing
Warn users that the first time they open a folder with many images after clearing the cache, it will be slower than usual as Windows regenerates every thumbnail.
5. Clear Icon Cache When Fixing Icon Issues
Thumbnail corruption and icon cache corruption often occur together. If clearing thumbnails doesn't fix the visual issue, clear the icon cache too (Method 2).
6. Use Non-Disruptive Cleanup for Automation
For scheduled maintenance or deployment, use Method 3 (next-login cleanup) to avoid disrupting active user sessions.
Conclusion
Clearing the thumbnail cache is a quick, non-destructive fix for visual inconsistencies in File Explorer. By properly managing the Explorer process, killing it to release file locks, waiting for handles to close, deleting the cache databases, and restarting the shell, you ensure a clean reset that forces Windows to regenerate accurate thumbnails from the actual file content. For environments where desktop disruption is unacceptable, the scheduled cleanup approach (Method 3) provides the same result without interrupting the user's current session.