← Back to Journal
閱讀中文版 →

I Let an AI Agent Clean My Mac's DiskFolder names lied, and a safety check silently checked nothing

My Mac mini's free space fell from 47 GB to 23 GB in eight days. I asked an AI coding agent to help find what was safe to delete. It found plenty, and it also nearly removed a cache that was in active use, because the folder's name said Legacy. These are the rules that came out of it.

Why the disk filled up so fast

Developer machines do not fill up evenly. They fill up in bursts, from tools that write large files and never clean up after themselves. On my machine there were three:

None of this is exotic. If you build apps or run AI agents on a Mac, you probably have some version of all three.

Rule 1: The AI proposes, a human approves

The agent never deletes on its own judgment. Before every cleanup it lists what it wants to remove, how big it is, and what could go wrong, and I choose. It also checks a written list of folders that must never be deleted: agent conversation histories, local AI models, voice training data, and every active project folder.

That list matters more than any clever script. Most of the things that hurt to lose look exactly like clutter.

Rule 2: Never trust a folder's name

We wrote a cleaner for the external drive that removed folders whose names contained words like old, retained or Legacy, after seven days. It sounded safe.

Then the checks found a folder called UvLegacy. Its name said "old". In reality, a Python package cache pointed straight at it through a symlink, and 35,996 files had been written into it in the previous seven days. A name-only rule would have deleted a cache that was in daily use.

The same thing happened in a batch of old migration copies. Nine of ten really were leftovers. The tenth looked identical but was the live workspace of one of my agents. Two symlinks pointed at it.

So the cleaner now deletes a folder only when it passes four checks at once:

  1. It is not in a protected area.
  2. No symlink anywhere points into it.
  3. Nothing inside it was written in the last seven days.
  4. No running process has it open (lsof).

Rule 3: Test scheduled scripts the way the scheduler runs them

Check 2, "no symlink points into it", turned out to be the hardest to get right. It took three attempts.

The third one is the dangerous kind of bug. The safety check did not crash. It reported "no links found", which reads as "everything is safe to delete". My manual test passed only because my interactive shell found a different find first.

The fix was to avoid both -L and -xtype: first list every symlink with a plain find -type l, then resolve the ones that point to folders with readlink -f and scan those too. It now collects about 1,776 live link targets in roughly 100 seconds. The script also logs that count, so if it ever drops to single digits I know the guard is broken before anything is deleted.

The lesson: run a scheduled script with /bin/bash and a minimal PATH before trusting it. Your terminal is not the environment it will run in.

Rule 4: A running cleaner is not proof it covers your disk

I already had a nightly job that cleared Xcode's DerivedData when space ran low. Its logs said it had reclaimed "about 0 GB" two days in a row. It was working exactly as written. It just measured free space on the internal disk, while the disk that was filling up was the external one.

For every cleanup job, check two things separately: which folders it actually touches, and which disk its "is space low?" check looks at.

Rule 5: Read the handoff note before deleting builds

Old builds can be rebuilt, so they feel safe to delete. But "old" depends on which one is current. Before removing 17 GB of game builds, the agent read the project's handoff note, which said in plain words which build was the latest and that everything older was obsolete. It kept that one and removed the rest. The build folder went from 18 GB to 1.1 GB.

Then we fixed the cause: the existing nightly cleaner had simply never included that build folder. One added line now keeps the three newest builds plus anything from the last two days.

Rule 6: Shrink, verify, then replace

The session logs with embedded screenshots could not simply be deleted; they are conversation history. Instead, a small script replaced each embedded image with a short placeholder, then compared the old and new files line by line to confirm every piece of text was identical, and only then swapped the new file in. Logs still being written were skipped. That recovered 5.25 GB across 98 files with no failures.

The root fix was turning off the screen-control feature that produced the screenshots when I was not using it.

Rule 7: Trust du, not df

On APFS, free space does not update the moment you delete. df can lag for a while. du on the folder you removed is the real proof that the delete worked. If space still does not come back, check for files that are deleted but still held open (lsof | grep deleted) and for local snapshots (tmutil listlocalsnapshots /).

What it added up to

The first round recovered about 22 GB. A later round on the external drive, after a file-by-file comparison of old migration backups, took it from 250 GB used to 158 GB. After each round, the agent confirmed that every agent workspace still opened and my shared knowledge base still answered queries.

Two things did not go as planned, and I think they are worth saying. A safety hook I had set up for the agent blocked it from running rm -rf on the temporary folder, so I cleared that myself (a restart also does it). And on one occasion about 25 GB of lost space was never explained, because the areas it was probably in needed permissions the agent did not have. I did not guess.

A short checklist

  1. Keep a written "never delete" list, and make the agent check it first.
  2. Judge a folder by what points to it and when it was last written, not by its name.
  3. Check for open files with lsof before deleting.
  4. Test scheduled cleanup scripts under /bin/bash with the scheduler's PATH.
  5. Confirm which disk a cleanup job measures.
  6. Read the project's notes to find the current build before removing old ones.
  7. Verify with du, then check that your tools still work.

An AI agent is good at finding large files. Deciding which large files are still alive is the part that needs rules.

macOSDisk SpaceClaude CodeCodexTroubleshootingAI Coding Agents

How this is made: these notes come from my own projects and experiments. I use AI assistants (Claude and Codex) heavily for drafting, translation and editing; I choose the topics and decide what gets published. — Michael Chuang