如何用脚本批量清理浏览器缓存?

wen 实用脚本 1

本文目录导读:

如何用脚本批量清理浏览器缓存?

  1. Chrome/Edge (Chromium 内核)
  2. Firefox 缓存清理
  3. 多浏览器批量清理脚本
  4. 使用快捷键和系统工具
  5. 高级清理脚本 - 包含更多选项
  6. 使用注意事项:
  7. 一键清理命令(Windows)

Chrome/Edge (Chromium 内核)

Windows PowerShell 脚本

# 清理Chrome缓存
$chromePath = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache"
$chromeCodeCache = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Code Cache"
# 清理缓存文件
if (Test-Path $chromePath) {
    Remove-Item "$chromePath\*" -Recurse -Force -ErrorAction SilentlyContinue
    Write-Host "Chrome缓存已清理"
}
if (Test-Path $chromeCodeCache) {
    Remove-Item "$chromeCodeCache\*" -Recurse -Force -ErrorAction SilentlyContinue
    Write-Host "Chrome代码缓存已清理"
}

Batch 批处理脚本

@echo off
echo 正在清理浏览器缓存...
:: Chrome缓存
if exist "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Cache" (
    rd /s /q "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Cache"
    mkdir "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Cache"
    echo Chrome缓存清理完成
)
:: Edge缓存
if exist "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Cache" (
    rd /s /q "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Cache"
    mkdir "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Cache"
    echo Edge缓存清理完成
)
echo 清理完成!
pause

Firefox 缓存清理

PowerShell 脚本

# Firefox缓存路径
$firefoxProfiles = Get-ChildItem "$env:APPDATA\Mozilla\Firefox\Profiles" -Directory
foreach ($profile in $firefoxProfiles) {
    $cachePath = Join-Path $profile.FullName "cache2"
    if (Test-Path $cachePath) {
        Remove-Item "$cachePath\*" -Recurse -Force -ErrorAction SilentlyContinue
        Write-Host "Firefox缓存已清理: $($profile.Name)"
    }
}

多浏览器批量清理脚本

完整 PowerShell 脚本 (推荐)

# 批量清理浏览器缓存脚本
Write-Host "开始清理浏览器缓存..." -ForegroundColor Green
# 需要关闭浏览器,否则文件可能被占用
$browsers = @(
    "chrome",
    "msedge", 
    "firefox",
    "brave",
    "opera"
)
$runningBrowsers = @()
foreach ($browser in $browsers) {
    $process = Get-Process -Name $browser -ErrorAction SilentlyContinue
    if ($process) {
        $runningBrowsers += $browser
    }
}
if ($runningBrowsers.Count -gt 0) {
    Write-Host "以下浏览器正在运行:$($runningBrowsers -join ', ')" -ForegroundColor Yellow
    $choice = Read-Host "是否关闭这些浏览器?(Y/N)"
    if ($choice -eq 'Y' -or $choice -eq 'y') {
        foreach ($browser in $runningBrowsers) {
            Stop-Process -Name $browser -Force -ErrorAction SilentlyContinue
            Start-Sleep -Seconds 2
        }
    }
}
# Chrome/Edge (Chromium)
$chromiumPaths = @(
    "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache",
    "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Code Cache",
    "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache",
    "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Code Cache",
    "$env:LOCALAPPDATA\BraveSoftware\Brave-Browser\User Data\Default\Cache"
)
foreach ($path in $chromiumPaths) {
    if (Test-Path $path) {
        try {
            Remove-Item "$path\*" -Recurse -Force -ErrorAction Stop
            Write-Host "清理成功: $path" -ForegroundColor Green
        } catch {
            Write-Host "清理失败: $path" -ForegroundColor Red
        }
    }
}
# Firefox
$firefoxProfiles = Get-ChildItem "$env:APPDATA\Mozilla\Firefox\Profiles" -Directory -ErrorAction SilentlyContinue
foreach ($profile in $firefoxProfiles) {
    $cachePath = Join-Path $profile.FullName "cache2"
    $startupCache = Join-Path $profile.FullName "startupCache"
    @($cachePath, $startupCache) | ForEach-Object {
        if (Test-Path $_) {
            try {
                Remove-Item "$_\*" -Recurse -Force -ErrorAction Stop
                Write-Host "清理成功: $_" -ForegroundColor Green
            } catch {
                Write-Host "清理失败: $_" -ForegroundColor Red
            }
        }
    }
}
Write-Host "缓存清理完成!" -ForegroundColor Green
# 可选:清理系统临时文件
$cleanTemp = Read-Host "是否同时清理系统临时文件?(Y/N)"
if ($cleanTemp -eq 'Y' -or $cleanTemp -eq 'y') {
    Remove-Item "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue
    Remove-Item "$env:WINDIR\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue
    Write-Host "系统临时文件已清理" -ForegroundColor Green
}
Read-Host "按回车键退出..."

使用快捷键和系统工具

浏览器自带清理功能

@echo off
echo 正在打开浏览器清理设置...
:: Chrome
start chrome://settings/clearBrowserData
:: Edge
start edge://settings/clearBrowserData
:: Firefox
start about:preferences#privacy
echo 请在打开的页面中设置清理选项
pause

高级清理脚本 - 包含更多选项

# 高级缓存清理脚本
param(
    [switch]$Silent,
    [switch]$AllBrowsers,
    [string[]]$BrowserList = @("Chrome", "Edge", "Firefox")
)
function Clear-BrowserCache {
    param([string]$BrowserName)
    switch ($BrowserName) {
        "Chrome" {
            $paths = @(
                "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache",
                "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Code Cache",
                "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Service Worker\CacheStorage"
            )
        }
        "Edge" {
            $paths = @(
                "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache",
                "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Code Cache"
            )
        }
        "Firefox" {
            $profile = Get-ChildItem "$env:APPDATA\Mozilla\Firefox\Profiles" -Directory | 
                       Select-Object -First 1
            if ($profile) {
                $paths = @(
                    Join-Path $profile.FullName "cache2",
                    Join-Path $profile.FullName "startupCache"
                )
            }
        }
    }
    foreach ($path in $paths) {
        if (Test-Path $path) {
            Remove-Item "$path\*" -Recurse -Force -ErrorAction SilentlyContinue
            if (-not $Silent) {
                Write-Host "$BrowserName 缓存已清理: $path" -ForegroundColor Green
            }
        }
    }
}
# 执行清理
if ($AllBrowsers) {
    $BrowserList = @("Chrome", "Edge", "Firefox")
}
foreach ($browser in $BrowserList) {
    Clear-BrowserCache -BrowserName $browser
}
if (-not $Silent) {
    Write-Host "清理完成!" -ForegroundColor Green
}

使用注意事项:

  1. 关闭浏览器:清理前最好关闭浏览器
  2. 管理员权限:某些路径可能需要管理员权限
  3. 备份重要数据:清理前确保没有未保存的浏览数据
  4. 选择性清理:可以修改脚本只清理特定浏览器的缓存

一键清理命令(Windows)

创建 .bat 文件,复制以下内容:

@echo off
echo 清理浏览器缓存...
del /f /s /q "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Cache\*.*" 2>nul
del /f /s /q "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Cache\*.*" 2>nul
echo 清理完成!
pause

选择适合你需求的脚本方案即可!

抱歉,评论功能暂时关闭!