文章 · 存档
我花了4小时删掉了488万个文件【我用的脚本 复制即用】
对。
你没看错,是488万个,一直藏在我电脑里的120G个没用的文件。
我的电脑用了4年了,微软的surface laptop 4,我很喜欢。硬盘512G当时觉得应该够用了。但其实用了2年的时候就已经几乎满了。
每天都在看我的硬盘如果还剩不到50G就会飘红,每天一飘红就想办法清理一下,没几天就有飘红了,于是再清理,就这么苦熬了2年,知道上个星期。
最近用AI频率很高,才发现AI是真占空间啊,当我看到我的硬盘不足20G的时候,我崩溃了。
我太疑惑了,为什么?我的硬盘是 512GB,我自己的文件才 120GB,已安装程序不到 50GB,windows的程序应该也就50G,那其他那些空间都去哪了?莫名其妙地就这么不清不楚不明不白的不见了?
我查啊查,我盯上了一个不起眼的路径:
C:\Users\......\Microsoft\Office\16.0\OfficeFileCache
就是它——OfficeFileCache。那里躺着不计其数的乱七八糟的文件,系统文件?不敢删。可是,真的不能删吗?
于是我就问AI,居然,AI说没事。果真?
于是,我下载了一个TreeSize Free,专门看文件夹大小的程序,乖乖,这个文件夹里居然有120G,这都啥啊。
你说能删,这么大,我万一删了,系统崩了,我找谁去?
可是我电脑的清理该做的都每天都做了,没用啊,咋办咋办
算了,就那么着吧,大不了重装系统。于是,我的行动开始了。
我先说结论:我怼的就是它——OfficeFileCache。
我把它清空了。488万个文件,120G,足足跑了4小时。
删完那一刻,我对着磁盘条发呆三秒,然后大笑三声:活了。
接下来是实录,没有大道理,只有情绪、操作和那段大家最关心的“代码”。
开干前的三分钟心理建设
第1分钟:骂一句——“你有什么权力给我塞这么多碎片?!”
第2分钟:装个 TreeSize Free,一看:C:\Users\…\Microsoft\Office\16.0\OfficeFileCache,120G。
第3分钟:问 AI:“能删吗?”——答:“缓存,能。”我:好,就你了。
友情提示:这堆是 Office + OneDrive/SharePoint/Teams 的缓存。删完会重建,第一次打开文档会重新下载,正常。
我不跟资源管理器讲道理
右键删除?呵呵,几百万个小文件能把资源管理器整出“禅定模式”。
要干净利落,就得看得到进度,否则人先崩溃。
于是我上了这一段带进度的 PowerShell(边扫边删、一路播报):
【我用的脚本 | 复制即用】
保存为 清理Office缓存_带进度.ps1,以管理员打开 PowerShell 后执行:
Set-ExecutionPolicy Bypass -Scope Process -Force
& "C:\完整路径\清理Office缓存_带进度.ps1"
# 一键清理 Office 缓存 (OfficeFileCache) —— 带扫描/删除进度
$ErrorActionPreference = 'SilentlyContinue'
Write-Host "======================================================"
Write-Host " Office 缓存清理工具 - 带进度"
Write-Host " 请先保存文档,并关闭 Office/Teams/OneDrive"
Write-Host "======================================================`n"
# 1) 关掉会占用缓存的家伙(不存在就忽略)
$procs = 'WINWORD','EXCEL','POWERPNT','ONENOTE','ONENOTEM','OUTLOOK','MSACCESS','VISIO','WINPROJ','FileCoAuth','MSOSYNC'
foreach($p in $procs){ Get-Process -Name $p -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue }
# 2) 找到缓存目录(优先 16.0,再试 15.0;你也可直接写死自己的路径)
$offcache = Join-Path $env:LOCALAPPDATA 'Microsoft\Office\16.0\OfficeFileCache'
if(-not (Test-Path $offcache)){
$offcache = Join-Path $env:LOCALAPPDATA 'Microsoft\Office\15.0\OfficeFileCache'
}
if(-not (Test-Path $offcache)){
Write-Host "未找到 OfficeFileCache 目录,请检查路径。" ; exit
}
Write-Host "即将清理:`n $offcache`n"
# 3) 扫描(不停汇报“已发现多少”)
Write-Host "正在扫描文件..."
$files = [System.Collections.Generic.List[string]]::new()
$dirs = [System.Collections.Generic.List[string]]::new()
$foundFiles = 0
foreach($f in [System.IO.Directory]::EnumerateFiles($offcache,'*',[System.IO.SearchOption]::AllDirectories)){
$files.Add($f); $foundFiles++
if($foundFiles % 1000 -eq 0){
Write-Progress -Activity "扫描文件" -Status "已发现 $foundFiles 个..." -PercentComplete 0
Write-Host ("[扫描中文件] 已发现 {0} 个..." -f $foundFiles)
}
}
Write-Progress -Activity "扫描文件" -Completed
Write-Host "[扫描中文件] 总计 $foundFiles 个。"
Write-Host "正在扫描文件夹..."
$foundDirs = 0
foreach($d in [System.IO.Directory]::EnumerateDirectories($offcache,'*',[System.IO.SearchOption]::AllDirectories)){
$dirs.Add($d); $foundDirs++
if($foundDirs % 1000 -eq 0){
Write-Progress -Activity "扫描文件夹" -Status "已发现 $foundDirs 个..." -PercentComplete 0
Write-Host ("[扫描中文件夹] 已发现 {0} 个..." -f $foundDirs)
}
}
Write-Progress -Activity "扫描文件夹" -Completed
Write-Host "[扫描中文件夹] 总计 $foundDirs 个。`n"
# 4) 删文件(每200个报一次进度)
$totalFiles = [math]::Max($files.Count,1)
$deleted = 0
Write-Host "开始删除文件(共 $($files.Count) 个)..."
foreach($f in $files){
try{ Remove-Item -LiteralPath $f -Force -ErrorAction SilentlyContinue } catch {}
$deleted++
if($deleted % 200 -eq 0 -or $deleted -eq $totalFiles){
$pct = [math]::Round(($deleted / $totalFiles) * 100, 1)
Write-Progress -Activity "删除文件" -Status "已删除 $deleted / $totalFiles ($pct%)" -PercentComplete $pct
Write-Host ("[删除中文件] {0}/{1} ({2}%%)" -f $deleted,$totalFiles,$pct)
}
}
Write-Progress -Activity "删除文件" -Completed
Write-Host "文件删除完成。`n"
# 5) 删空目录(从深到浅,同样播报)
$dirsSorted = $dirs | Sort-Object { $_.Length } -Descending
$totalDirs = [math]::Max($dirsSorted.Count,1)
$removed = 0
Write-Host "开始删除空文件夹(共 $($dirsSorted.Count) 个)..."
foreach($d in $dirsSorted){
try{ Remove-Item -LiteralPath $d -Force -ErrorAction SilentlyContinue } catch {}
$removed++
if($removed % 200 -eq 0 -or $removed -eq $totalDirs){
$pct = [math]::Round(($removed / $totalDirs) * 100, 1)
Write-Progress -Activity "删除文件夹" -Status "已删除 $removed / $totalDirs ($pct%)" -PercentComplete $pct
Write-Host ("[删除中文件夹] {0}/{1} ({2}%%)" -f $removed,$totalDirs,$pct)
}
}
Write-Progress -Activity "删除文件夹" -Completed
Write-Host "`n完成!首次打开云端文档会重新下载,属正常。"
【要双击的同学 | 启动器 .bat】
保存为 清理Office缓存_启动器.bat(ANSI 编码),双击就跑:
@echo off
set "PS=C:\完整路径\清理Office缓存_带进度.ps1"
powershell -NoProfile -ExecutionPolicy Bypass -File "%PS%"
pause
四小时的弹幕
:30 —— “会不会崩?”我怂了一下,继续。
:45 —— 计数器破 100 万,我开始怀疑人生:谁造的?
:20 —— 一边删一边刷“已删除 200/400/600…”,我安心了:它在动。
:10 —— 我去泡了杯咖啡,回来它还在稳稳推进。
:00 —— “完成”的瞬间,磁盘+120GB。惴惴不安,电脑不会直接崩吧。
:01—— 重启电脑,一切如故,虚惊一场。
给同路人的三条私货
别用资源管理器硬删,看不到进度会把你逼疯。
一定要先退 OneDrive/Teams/Office,不然“正在使用”会拖慢。
这是缓存。删完会重建,别慌。第一次打开文档慢点儿,正常。
想更狠?清完去「索引选项-高级-重建」顺手瘦下搜索索引(Windows.edb),再把休眠关了(powercfg -h off)顺便省十几 G——但这俩是番外,今天主角是 OfficeFileCache。
把话撂在这
我不劝谁动刀,也不替你背锅。
我只是把自己的战绩摆出来:488 万个,4 小时,+120GB。
要不要试,你自己决定;真要试,就用带进度的那段脚本,别跟小文件海洋讲道理。
评论区欢迎晒战果:
你们的 OfficeFileCache 到底能胖成啥样?谁更离谱,今晚见分晓。
I spent 4 hours to delete 4.88 million files [my script | copy and use]
Yes.
You read it right, 4.88 million files, hiding in my computer, 120G of useless files.
My computer has been used for 4 years, Microsoft Surface Laptop 4, I like it.
The hard disk is 512G, at that time I thought it should be enough.
But after using for 2 years, it was almost full.
Every day I saw my hard disk turn red if less than 50G left.
Every time it turned red, I cleaned up a bit. After a few days, red again. Then clean again.
In this way I struggled for 2 years, until last week.
Recently I use AI very often, then I found out AI really takes space.
When I saw my hard disk had less than 20G left, I broke down.
I was very puzzled, why?
My hard disk is 512GB.
My own files are only 120GB.
Installed programs less than 50GB.
Windows system maybe 50GB.
Then where did all other space go?
It just disappeared, unclear, without reason.
I searched and searched, I stared at one small path:
C:\Users......\Microsoft\Office\16.0\OfficeFileCache
It was this—OfficeFileCache.
Inside were countless messy files. System files?
I did not dare to delete. But, really cannot delete?
So I asked AI, and AI said it is fine. Really?
Then I downloaded TreeSize Free, a program to check folder size.
Wow, this folder had 120G, full of stuff.
You say it can be deleted, but so big, what if I delete and system crashes? Who can I ask?
But I had done all cleaning tools every day, no use. What to do, what to do.
Forget it, let it be, at worst reinstall system. So I started action.
First say result:
The one I attacked is OfficeFileCache.
I cleared it. 4.88 million files, 120G, took 4 hours.
After deleting, I stared at the disk bar for 3 seconds, then laughed loudly 3 times: Alive again.
Next is the record, no big theory, only feelings, actions, and the “code” everyone cares about.
Three minutes of mental build before action
Minute 1: curse once—“Who gave you the right to stuff me with so many fragments?!”
Minute 2: install TreeSize Free, see: C:\Users\…\Microsoft\Office\16.0\OfficeFileCache, 120G.
Minute 3: ask AI: “Can I delete?” — Answer: “Cache, yes.” Me: OK, you are the one.
Friendly note: this pile is cache of Office + OneDrive/SharePoint/Teams.
After delete it will rebuild. First time open doc will re-download, normal.
I don't argue with File Explorer
Right click delete? Haha, millions of small files will make Explorer go “Zen mode.”
To be clean and fast, you must see progress, or you crash first.
So I used this PowerShell script with progress (scan and delete, report all the way):
[my script | copy and use]
Save as “CleanOfficeCache_WithProgress.ps1”, open PowerShell as admin and run:
Set-ExecutionPolicy Bypass -Scope Process -Force
& "C:\full_path\CleanOfficeCache_WithProgress.ps1"
(script content same as original, not translated)
[for double click people | launcher .bat]
Save as “CleanOfficeCache_Launcher.bat” (ANSI code), double click to run:
@echo off
set "PS=C:\full_path\CleanOfficeCache_WithProgress.ps1"
powershell -NoProfile -ExecutionPolicy Bypass -File "%PS%"
pause
Four hours of bullet screen
:30 — “Will it crash?” I got scared, but kept going.
:45 — counter broke 1 million, I doubted life: who made this?
:20 — deleting and showing “deleted 200/400/600…”, I felt safe: it is moving.
:10 — I made a coffee, came back, still moving steady.
:00 — at “finished”, disk +120GB. Worried, will PC crash?
:01 — restart PC, all normal, false alarm.
Three tips for fellow travelers
Don't use Explorer to hard delete, no progress will drive you mad.
Must quit OneDrive/Teams/Office first, or “in use” will slow down.
This is cache. After delete will rebuild, don't panic. First open doc is slower, normal.
Want more? After clean, go to “Index Options - Advanced - Rebuild” to shrink search index (Windows.edb). Then turn off hibernate (powercfg -h off) to save 10G+. But these are side story, today's main is OfficeFileCache.
Final words
I don't push anyone to try, and I don't take blame.
I just show my result: 4.88 million, 4 hours, +120GB.
Try or not, you decide.
If try, use the script with progress, don't argue with ocean of small files.
Comments welcome:
How fat is your OfficeFileCache? Who is crazier? See tonight.