本文目录导读:

Windows 批处理脚本 (.bat)
适用于 Chrome、Edge、Firefox 等主流浏览器的缓存目录清理。
@echo off
chcp 65001 >nul浏览器缓存自动清理工具
color 0a
echo ============================================
echo 正在清理浏览器缓存文件...
echo ============================================
:: 清理 Chrome 缓存
echo [1/3] 清理 Chrome 缓存...
if exist "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Cache" (
del /f /s /q "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Cache\*.*" >nul 2>&1
for /d %%i in ("%LOCALAPPDATA%\Google\Chrome\User Data\Default\Cache\*") do rd /s /q "%%i" 2>nul
echo Chrome 缓存清理完成。
) else (
echo Chrome 缓存目录不存在,已跳过。
)
:: 清理 Edge 缓存
echo [2/3] 清理 Edge 缓存...
if exist "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Cache" (
del /f /s /q "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Cache\*.*" >nul 2>&1
for /d %%i in ("%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Cache\*") do rd /s /q "%%i" 2>nul
echo Edge 缓存清理完成。
) else (
echo Edge 缓存目录不存在,已跳过。
)
:: 清理 Firefox 缓存(通过 profiles.ini 定位)
echo [3/3] 清理 Firefox 缓存...
if exist "%APPDATA%\Mozilla\Firefox\profiles.ini" (
for /f "tokens=2 delims==" %%a in ('type "%APPDATA%\Mozilla\Firefox\profiles.ini" ^| find "Path="') do (
set "ffpath=%%a"
setlocal enabledelayedexpansion
if exist "!APPDATA!\Mozilla\Firefox\!ffpath!\cache2" (
del /f /s /q "!APPDATA!\Mozilla\Firefox\!ffpath!\cache2\*.*" >nul 2>&1
for /d %%i in ("!APPDATA!\Mozilla\Firefox\!ffpath!\cache2\*") do rd /s /q "%%i" 2>nul
)
endlocal
)
echo Firefox 缓存清理完成。
) else (
echo Firefox 配置文件不存在,已跳过。
)
echo ============================================
echo 清理操作已全部完成!
echo ============================================
pause
使用方法:将上述代码保存为 clear_cache.bat,右键以管理员身份运行(部分浏览器的缓存受权限保护)。
macOS / Linux Shell 脚本 (.sh)
适用于 macOS 和 Linux 系统,支持 Chrome、Firefox、Brave、Vivaldi 等。
#!/bin/bash
echo "===================================="
echo " 开始清理浏览器缓存..."
echo "===================================="
# 清理 Chrome / Chromium 缓存
echo "[1/3] 清理 Chrome 缓存..."
if [ -d "$HOME/Library/Caches/Google/Chrome" ]; then # macOS 路径
rm -rf "$HOME/Library/Caches/Google/Chrome/Default/Cache" 2>/dev/null
rm -rf "$HOME/Library/Caches/Google/Chrome/Default/Code Cache" 2>/dev/null
echo " macOS Chrome 清理完成。"
elif [ -d "$HOME/.cache/google-chrome" ]; then # Linux 路径
rm -rf "$HOME/.cache/google-chrome/Default/Cache" 2>/dev/null
rm -rf "$HOME/.cache/google-chrome/Default/Code Cache" 2>/dev/null
echo " Linux Chrome 清理完成。"
else
echo " Chrome 缓存目录不存在,已跳过。"
fi
# 清理 Firefox 缓存
echo "[2/3] 清理 Firefox 缓存..."
# 查找所有 Firefox profile 中的 cache2 目录
find "$HOME/Library/Caches/Firefox" -type d -name "cache2" 2>/dev/null | while read dir; do
rm -rf "$dir"/*
echo " 已清理: $dir"
done
# Linux 备用路径
find "$HOME/.cache/mozilla/firefox" -type d -name "cache2" 2>/dev/null | while read dir; do
rm -rf "$dir"/*
done
echo " Firefox 缓存清理完成。"
# 清理 Safari (仅 macOS)
echo "[3/3] 清理 Safari 缓存 (macOS)..."
if [ -d "$HOME/Library/Caches/com.apple.Safari" ]; then
rm -rf "$HOME/Library/Caches/com.apple.Safari/*" 2>/dev/null
echo " Safari 缓存清理完成。"
else
echo " Safari 目录不存在 (可能为 Linux 或未安装),已跳过。"
fi
echo "===================================="
echo " 全部清理完毕!"
echo "===================================="
使用方法:
chmod +x clean_cache.sh # 赋予执行权限 ./clean_cache.sh # 运行脚本
Python 脚本 (跨平台最灵活)
利用 Python 的 os 和 platform 模块,自动识别系统并清理。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import platform
import shutil
def clean_chrome_cache():
system = platform.system()
cache_paths = {
'Windows': os.path.expandvars(r'%LOCALAPPDATA%\Google\Chrome\User Data\Default\Cache'),
'Darwin': os.path.expanduser('~/Library/Caches/Google/Chrome/Default/Cache'), # macOS
'Linux': os.path.expanduser('~/.cache/google-chrome/Default/Cache')
}
path = cache_paths.get(system)
if path and os.path.exists(path):
shutil.rmtree(path, ignore_errors=True)
os.makedirs(path, exist_ok=True)
print(f"[OK] Chrome 缓存已清理: {path}")
else:
print("[SKIP] Chrome 缓存目录未找到,跳过。")
def clean_firefox_cache():
system = platform.system()
if system == 'Windows':
profiles = os.path.expandvars(r'%APPDATA%\Mozilla\Firefox\Profiles')
elif system == 'Darwin':
profiles = os.path.expanduser('~/Library/Caches/Firefox')
elif system == 'Linux':
profiles = os.path.expanduser('~/.cache/mozilla/firefox')
else:
print("[SKIP] 未知系统,跳过 Firefox。")
return
if os.path.exists(profiles):
for root, dirs, files in os.walk(profiles):
if root.endswith('cache2') or 'cache2' in dirs:
cache2_dir = os.path.join(root, 'cache2') if root.endswith('cache2') else root
shutil.rmtree(cache2_dir, ignore_errors=True)
os.makedirs(cache2_dir, exist_ok=True)
print(f"[OK] Firefox 缓存已清理: {cache2_dir}")
else:
print("[SKIP] Firefox 缓存目录未找到。")
def main():
print("=" * 40)
print(" 跨平台缓存清理工具")
print("=" * 40)
clean_chrome_cache()
clean_firefox_cache()
print("=" * 40)
print(" 清理完成!")
print("=" * 40)
if __name__ == "__main__":
main()
使用方法:
python3 clean_cache.py # 或 python clean_cache.py(取决于您的Python版本)
高级技巧:定时自动运行
| 系统 | 实现方式 | 示例 |
|---|---|---|
| Windows | 任务计划程序 | 创建基本任务,触发器设置为“每天”或“登录时”,操作选择运行您的 .bat 脚本 |
| macOS | launchd / crontab | crontab -e 添加一行:0 9 * * * /path/to/clean_cache.sh(每天早上9点运行) |
| Linux | cron | crontab -e 添加:@daily /path/to/clean_cache.sh |
重要提醒
- 备份数据:首次运行前建议手动备份书签/密码(使用浏览器内置同步功能)。
- 浏览器需关闭:部分缓存文件在浏览器运行时被锁定,无法删除,脚本会自动跳过被锁定的文件,但最彻底的方式是关闭浏览器后运行。
- 扩展缓存:有些浏览器扩展(如
Adobe Flash、Unity)可能使用独立缓存目录,可参考上述逻辑添加。 - 隐私保护:该脚本仅清除缓存文件,不会清除Cookie、历史记录或密码,如需更全面的清理,可额外添加相关目录(但请谨慎操作)。
您可以根据需要选择最合适的脚本,并自定义要清理的浏览器和目录。