Juraj Blahunka

Claude Code is quietly filling your disk

11 min read

A cartoon creature biting into the macOS storage bar, 925.5 of 994.66 GB used, above a terminal where du reports 97G in /private/tmp/claude-501

I was building Note to Self, my iOS app, with several Claude Code agents when my Mac suddenly hit 93% full. The culprit? One hidden folder, /private/tmp/claude-501, quietly holding 97 GB.

Every Claude Code session gets its own temp folder, and every build, test run, screenshot and screen recording lands in it. Claude Code never cleans it up. macOS only clears files nobody has touched for 3 days, and Finder hides the folder, so you never see it grow. One iOS build is about 5 GB, so a few busy agents pile up tens of gigabytes long before anything is old enough to go.

Check yours right now:

du -sh /private/tmp/claude-$(id -u)

Is the number big? Read on: the rest of this post is the hook that cleans it up for you, automatically.

Why iOS makes it worse

On a web project, a forgotten temp folder is a few megabytes of logs. On an iOS project, it is a build.

Agents that run several builds at once give each one its own -derivedDataPath inside the temp folder, so they never fight over Xcode’s shared DerivedData. That is the right call for parallel work, and each of those folders is about 5 GB. Every UI test run adds an .xcresult bundle of 50 to 150 MB, and screen recordings of the simulator add more. Several agents, several builds each, every day, and the folder grows by the hour.

The hook

Claude Code runs a SessionStart hook whenever a session starts, which is a good moment to clean up after the sessions before it. In ~/.claude/settings.json:

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/hooks/clean-tmp.sh 2>/dev/null || true",
            "timeout": 30
          }
        ]
      }
    ]
  }
}

The || true matters: a cleanup script should never be the reason a session fails to start.

The script applies different rules to different kinds of files:

The whole script is at the end of this post. These are the parts that took more than one try.

Never touch a running session

Deleting the temp folder of a session that is still working breaks it in confusing ways, so the script first finds out which sessions are alive. Claude Code keeps a file per running process in ~/.claude/sessions/<pid>.json with the session id in it. If the pid is still alive, the session is too:

live=" "
for f in "$HOME"/.claude/sessions/*.json; do
  [ -f "$f" ] || continue
  pid=$(basename "$f" .json)
  kill -0 "$pid" 2>/dev/null || continue
  id=$(sed -n 's/.*"sessionId":"\([^"]*\)".*/\1/p' "$f")
  [ -n "$id" ] && live="$live$id "
done

That file is an undocumented internal of Claude Code, so check it still looks like this on your version. A live session only ever loses its heavy build output, and only once it has not been touched for 2 hours.

“Untouched” means the newest file

My first version asked find whether the folder itself was older than the cutoff. That is wrong in both directions. A folder’s mtime does not change when a file deeper inside it is written, so a build that was busy a minute ago can look abandoned. And it does change when something inside it is deleted, so the hook’s own deletions kept making folders look fresh.

So the age check looks at the newest file inside:

# True when no file under $1 changed in the last $2 minutes, and $1 itself is that old too
# (so a just-created, still empty folder is never taken for an abandoned one).
untouched_for() {
  [ -z "$(find "$1" -maxdepth 0 -mmin -"$2" -print 2>/dev/null)" ] &&
    [ -z "$(find "$1" -type f -mmin -"$2" -print -quit 2>/dev/null)" ]
}

-print -quit stops at the first recent file it finds, so a busy 5 GB folder costs almost nothing to check.

Finding DerivedData wherever agents put it

Agents name their DerivedData folders whatever they like: dd, build, derived, a single letter. Matching names would miss most of them. A DerivedData folder always contains Index.noindex or Build/Intermediates.noindex, so the script looks for those and takes the parent:

find "$1" \( -name Index.noindex -o -path '*/Build/Intermediates.noindex' \) -prune -print 2>/dev/null |
  sed -e 's#/Index.noindex$##' -e 's#/Build/Intermediates.noindex$##' | sort -u |
  while read -r d; do
    [ -d "$d" ] && untouched_for "$d" $heavy_minutes && remove "$d"
  done

Result bundles inside a build’s own Logs/Test are left alone, since they go when that build goes.

The same idea cleans Xcode’s shared DerivedData in ~/Library/Developer/Xcode/DerivedData. Each folder there has an info.plist that records the workspace it was built from. When that path no longer exists, nobody will ever build it again:

# PlistBuddy prints its error to stdout when the file is missing, so read it only when it exists
ws=
[ -f "$d/info.plist" ] && ws=$(/usr/libexec/PlistBuddy -c 'Print :WorkspacePath' "$d/info.plist" 2>/dev/null)
if [ -n "$ws" ]; then
  [ -e "$ws" ] || remove "$d"
fi

The -f check matters more than it looks. When the file is missing, PlistBuddy prints its error to stdout, so ws holds an error message, the “workspace” does not exist, and the folder is deleted. Xcode creates a DerivedData folder before it writes info.plist into it, so my first version could delete a brand new folder while Xcode was still setting it up, and my log shows it did, three times.

With agents working in short-lived git worktrees, this rule alone freed 20 GB in its first two days.

Stay out of the way

A session start should not wait on du and find over tens of gigabytes. The script starts a copy of itself in the background and returns at once:

if [ -z "$CLEAN_TMP_BG" ] && [ -z "$DRY_RUN" ]; then
  CLEAN_TMP_BG=1 nohup "$0" >/dev/null 2>&1 &
  exit 0
fi

That created the next problem. Starting four agents at once starts four cleanups, all walking the same folders and racing to delete the same builds. A lock keeps it to one run at a time. mkdir is atomic, so it works as a lock without any extra tools, and a lock older than an hour belongs to a run that died:

if ! mkdir "$lock" 2>/dev/null; then
  [ -n "$(find "$lock" -maxdepth 0 -mmin +60)" ] || exit 0
  rm -rf "$lock"; mkdir "$lock" || exit 0
fi
trap 'rm -rf "$lock"' EXIT

Trust, but log

A script that runs rm -rf on its own deserves two safety nets. DRY_RUN=1 prints what it would delete and deletes nothing, so you can see what it would do on your machine before trusting it:

DRY_RUN=1 ~/.claude/hooks/clean-tmp.sh

And every real deletion is written to ~/.claude/hooks/clean-tmp.log with its size, so when something goes missing, there is a record of what took it:

2026-11-23 10:21  95M /private/tmp/claude-501/<project>/<session>/scratchpad/ui-tests.xcresult

Not an iOS developer?

Most of the script is about Xcode, and you can drop those parts. What is left is the session folder rule, which is what my original prompt asked for, and it is enough for most projects. You can also hand Claude a sentence instead of a script:

Add a SessionStart hook that deletes Claude Code’s temp folders once nothing in them has changed for 8 hours. Skip sessions that are still running.

With the hook running, the folder on my Mac sits at about 13 GB in the middle of a busy day, down from 97. Run the du command above and see what yours says.

The whole script

Save it as ~/.claude/hooks/clean-tmp.sh and make it executable with chmod +x.

#!/bin/sh
# SessionStart hook: keeps Claude Code's temp folders and Xcode's build output from filling the disk.
#
# 1. Session folders (/private/tmp/claude-$UID/<project>/<session-id>) of sessions that are
#    no longer running are deleted once nothing in them has changed for 12 hours.
# 2. Heavy build output (an Xcode DerivedData folder, an .xcresult bundle, a video over
#    10 MB) is deleted once it has not changed for 2 hours while its session still runs, and
#    for 1 hour once its session has ended: a build is cheap to redo, and agents make several
#    5 GB builds a day. Heavy output loose in the temp root follows the 2-hour rule.
# 3. Anything else left loose in the temp root is deleted once untouched for 12 hours.
#    Session folders of ended sessions that hold no files are deleted once 10 minutes old.
# 4. Xcode's DerivedData folders whose workspace no longer exists (deleted worktrees and
#    scratch checkouts) are deleted, as are ones with no info.plist untouched for 12 hours.
# 5. Simulators whose runtime is gone are deleted.
#
# "Untouched" checks the newest file inside, never folder mtimes: a folder's own mtime does
# not change when files deeper in it are written, and it does change when something inside
# is deleted, so counting folders let this hook's own deletions keep a build looking fresh.
# It runs in the background so session start never waits on it, one run at a time;
# DRY_RUN=1 prints what it would delete instead.
# Each deletion is logged to ~/.claude/hooks/clean-tmp.log.

if [ -z "$CLEAN_TMP_BG" ] && [ -z "$DRY_RUN" ]; then
  CLEAN_TMP_BG=1 nohup "$0" >/dev/null 2>&1 &
  exit 0
fi

root="/private/tmp/claude-$(id -u)"
log="$HOME/.claude/hooks/clean-tmp.log"
lock="/private/tmp/clean-tmp-$(id -u).lock"
live_heavy_minutes=120    # build output of running sessions: 2 hours
ended_heavy_minutes=60    # build output of ended sessions: 1 hour
idle_hours=12

# One run at a time; a lock older than an hour is from a run that died.
if ! mkdir "$lock" 2>/dev/null; then
  [ -n "$(find "$lock" -maxdepth 0 -mmin +60)" ] || exit 0
  rm -rf "$lock"; mkdir "$lock" || exit 0
fi
trap 'rm -rf "$lock"' EXIT

remove() {
  if [ -n "$DRY_RUN" ]; then
    echo "would delete $(du -sh "$1" 2>/dev/null | cut -f1) $1"
  else
    echo "$(date '+%Y-%m-%d %H:%M') $(du -sh "$1" 2>/dev/null | cut -f1) $1" >> "$log"
    rm -rf "$1"
  fi
}

# True when no file under $1 changed in the last $2 minutes, and $1 itself is that old too
# (so a just-created, still empty folder is never taken for an abandoned one).
untouched_for() {
  [ -z "$(find "$1" -maxdepth 0 -mmin -"$2" -print 2>/dev/null)" ] &&
    [ -z "$(find "$1" -type f -mmin -"$2" -print -quit 2>/dev/null)" ]
}

# Session ids of Claude Code processes still running.
live=" "
for f in "$HOME"/.claude/sessions/*.json; do
  [ -f "$f" ] || continue
  pid=$(basename "$f" .json)
  kill -0 "$pid" 2>/dev/null || continue
  id=$(sed -n 's/.*"sessionId":"\([^"]*\)".*/\1/p' "$f")
  [ -n "$id" ] && live="$live$id "
done

# Heavy build output under $1, untouched for $2 minutes.
prune_heavy() {
  heavy_minutes=$2
  # DerivedData folders: the parent of an Index.noindex or Build/Intermediates.noindex.
  find "$1" \( -name Index.noindex -o -path '*/Build/Intermediates.noindex' \) -prune -print 2>/dev/null |
    sed -e 's#/Index.noindex$##' -e 's#/Build/Intermediates.noindex$##' | sort -u |
    while read -r d; do
      [ -d "$d" ] && untouched_for "$d" $heavy_minutes && remove "$d"
    done
  # Result bundles a build keeps in its own Logs/Test go with that build, never on their own.
  find "$1" -name '*.xcresult' -prune -print 2>/dev/null | grep -v '/Logs/Test/' | while read -r d; do
    [ -d "$d" ] && untouched_for "$d" $heavy_minutes && remove "$d"
  done
  find "$1" -type f \( -iname '*.mp4' -o -iname '*.mov' \) -size +10M -mmin +$heavy_minutes 2>/dev/null |
    while read -r f; do remove "$f"; done
}

if [ -d "$root" ]; then
  for item in "$root"/* "$root"/.[!.]*; do
    [ -e "$item" ] || continue
    name=$(basename "$item")
    case "$name" in bash-edit-diff|bundled-skills) continue ;; esac

    if [ -d "$item" ] && [ -n "$(find "$item" -mindepth 1 -maxdepth 1 -type d -name '????????-????-????-????-????????????' -print -quit)" ]; then
      # A project folder of session folders.
      for session in "$item"/*/; do
        session=${session%/}
        [ -d "$session" ] || continue
        case "$live" in *" $(basename "$session") "*) prune_heavy "$session" $live_heavy_minutes; continue ;; esac
        if [ -z "$(find "$session" -type f -print -quit 2>/dev/null)" ] &&
          [ -n "$(find "$session" -maxdepth 0 -mmin +10 -print 2>/dev/null)" ]; then
          # No files at all (only empty folders such as started/ and tasks/), and over 10 minutes old.
          if [ -n "$DRY_RUN" ]; then echo "would delete empty $session"; else rm -rf "$session"; fi
        elif untouched_for "$session" $((idle_hours * 60)); then
          remove "$session"
        else
          prune_heavy "$session" $ended_heavy_minutes
        fi
      done
    else
      # Loose in the temp root: build output after 2 hours, the rest after 12 hours.
      if untouched_for "$item" $((idle_hours * 60)); then
        remove "$item"
      else
        prune_heavy "$item" $live_heavy_minutes
      fi
    fi
  done
  [ -n "$DRY_RUN" ] || find "$root" -mindepth 1 -maxdepth 1 -type d -empty -delete
fi

# Xcode DerivedData of workspaces that no longer exist.
dd="$HOME/Library/Developer/Xcode/DerivedData"
for d in "$dd"/*-*/; do
  d=${d%/}
  [ -d "$d" ] || continue
  case "$(basename "$d")" in *.noindex) continue ;; esac
  # PlistBuddy prints its error to stdout when the file is missing, so read it only when it exists
  ws=
  [ -f "$d/info.plist" ] && ws=$(/usr/libexec/PlistBuddy -c 'Print :WorkspacePath' "$d/info.plist" 2>/dev/null)
  if [ -n "$ws" ]; then
    [ -e "$ws" ] || remove "$d"
  elif untouched_for "$d" $((idle_hours * 60)); then
    remove "$d"
  fi
done

# Simulators whose runtime was removed.
[ -n "$DRY_RUN" ] || xcrun simctl delete unavailable >/dev/null 2>&1

# Keep the log short.
[ -f "$log" ] && tail -n 2000 "$log" > "$log.tmp" && mv "$log.tmp" "$log"
exit 0