本文目录导读:

我来帮你写一个实用的缓存清理脚本,根据不同需求,我提供几种常见方案:
通用缓存清理脚本 (Windows)
批处理脚本 (.bat)
@echo off缓存清理工具 echo ================================= echo 正在清理系统缓存... echo ================================= :: 关闭回显 echo 1. 清理临时文件夹... del /f /s /q %TEMP%\*.* >nul 2>&1 del /f /s /q C:\Windows\Temp\*.* >nul 2>&1 del /f /s /q C:\Windows\Prefetch\*.* >nul 2>&1 echo 2. 清理回收站... rd /s /q C:\$Recycle.bin >nul 2>&1 echo 3. 清理浏览器缓存... :: Chrome rd /s /q "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Cache" >nul 2>&1 :: Edge rd /s /q "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Cache" >nul 2>&1 :: Firefox rd /s /q "%APPDATA%\Mozilla\Firefox\Profiles\*\cache2" >nul 2>&1 echo 4. 清理DNS缓存... ipconfig /flushdns >nul echo 5. 清理系统缓存 (需要管理员权限)... cleanmgr /sageset:1 /sagerun:1 >nul echo ================================= echo 清理完成! echo ================================= pause
PowerShell 脚本 (.ps1)
Write-Host "正在清理缓存..." -ForegroundColor Green
# 清理临时文件
Remove-Item "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "C:\Windows\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue
# 清理浏览器缓存
$browsers = @(
"$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache",
"$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache",
"$env:APPDATA\Mozilla\Firefox\Profiles\*\cache2"
)
foreach ($path in $browsers) {
if (Test-Path $path) {
Remove-Item "$path\*" -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "已清理: $path" -ForegroundColor Yellow
}
}
# 清理DNS缓存
Clear-DnsClientCache
Write-Host "清理完成!" -ForegroundColor Green
Linux 缓存清理脚本
#!/bin/bash echo "开始清理缓存..." # 清理APT缓存 sudo apt-get clean 2>/dev/null || sudo yum clean all 2>/dev/null # 清理包管理器缓存 sudo apt-get autoremove -y 2>/dev/null || echo "非Ubuntu系统跳过" # 清理系统日志缓存 sudo journalctl --vacuum-time=3d 2>/dev/null # 清理用户缓存 rm -rf ~/.cache/* 2>/dev/null # 清理浏览器缓存 (Chrome) rm -rf ~/.cache/google-chrome/* 2>/dev/null # 清理缩略图缓存 rm -rf ~/.thumbnails/* 2>/dev/null rm -rf ~/.cache/thumbnails/* 2>/dev/null # 清理内存缓存 (谨慎使用) sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches >/dev/null 2>&1 echo "缓存清理完成!"
macOS 缓存清理脚本
#!/bin/bash echo "清理macOS缓存..." # 清理用户缓存 rm -rf ~/Library/Caches/* 2>/dev/null # 清理系统缓存 (需要密码) sudo rm -rf /Library/Caches/* 2>/dev/null # 清理浏览器缓存 rm -rf ~/Library/Caches/Google/Chrome/* 2>/dev/null rm -rf ~/Library/Caches/Firefox/* 2>/dev/null rm -rf ~/Library/Caches/com.apple.Safari/* 2>/dev/null # 清理DNS缓存 sudo dscacheutil -flushcache sudo killall -HUP mDNSResponder echo "清理完成!"
进阶版:Python 缓存清理脚本
#!/usr/bin/env python3
import os
import shutil
import platform
def clean_windows():
"""Windows缓存清理"""
temp_dirs = [
os.environ.get('TEMP'),
os.environ.get('TMP'),
'C:\\Windows\\Temp',
os.path.expandvars('%LOCALAPPDATA%\\Temp')
]
for dir_path in temp_dirs:
if dir_path and os.path.exists(dir_path):
try:
shutil.rmtree(dir_path, ignore_errors=True)
print(f"已清理: {dir_path}")
except:
print(f"无法清理: {dir_path}")
def clean_linux():
"""Linux缓存清理"""
cache_dirs = [
os.path.expanduser('~/.cache'),
'/tmp',
'/var/tmp'
]
for dir_path in cache_dirs:
if os.path.exists(dir_path):
for item in os.listdir(dir_path):
item_path = os.path.join(dir_path, item)
try:
if os.path.isfile(item_path):
os.remove(item_path)
elif os.path.isdir(item_path):
shutil.rmtree(item_path, ignore_errors=True)
except:
pass
print("Linux缓存清理完成")
def clean_macos():
"""macOS缓存清理"""
cache_dirs = [
os.path.expanduser('~/Library/Caches'),
'/Library/Caches'
]
for dir_path in cache_dirs:
if os.path.exists(dir_path):
for item in os.listdir(dir_path):
item_path = os.path.join(dir_path, item)
if os.path.isdir(item_path):
try:
shutil.rmtree(item_path, ignore_errors=True)
except:
pass
print("macOS缓存清理完成")
def main():
system = platform.system()
print(f"正在清理 {system} 系统缓存...")
if system == "Windows":
clean_windows()
elif system == "Linux":
clean_linux()
elif system == "Darwin":
clean_macos()
else:
print("不支持的操作系统")
if __name__ == "__main__":
main()
重要提示
- 备份重要数据:清理前确认没有重要文件在缓存目录
- 管理员权限:部分清理操作需要管理员/root权限
- 谨慎清理:建议只清理用户级缓存,系统关键缓存不要随意清理
- 定期清理:建议每月清理1-2次即可,太频繁可能影响性能
- 测试环境:在正式环境使用前,先在测试环境验证
选择适合你系统的脚本,保存为对应格式的文件即可运行,需要根据实际情况调整清理范围。