worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

After removing a worktree, there is a remaining folder with `removing` - why?

Open ortonomy opened this issue 8 months ago • 8 comments

example: workstream-mono.payr-0-wt-test.wt-removing-1773715045

why is this left in my folder. Is there some background process that failed to complete?

ortonomy avatar Mar 17 '26 02:03 ortonomy

Thanks for reporting this! I was able to reproduce the issue and identified the root cause: when wt remove renames the worktree directory to a staging path (.wt-removing-<timestamp>) but the background rm -rf process doesn't complete (e.g., process killed, system shutdown), the staging directory is left behind with no cleanup mechanism.

I've opened #1574 with a fix that cleans up stale .wt-removing-* directories opportunistically before each new removal. A maintainer will review it shortly.

worktrunk-bot avatar Mar 17 '26 02:03 worktrunk-bot

Oh, it did eventually remove - it just took a really long time...

ortonomy avatar Mar 17 '26 03:03 ortonomy

Good to know it did complete! That's actually the expected behavior — wt remove renames the directory to a staging path (.wt-removing-*) first so you can keep working immediately, then removes it in the background. For large worktrees with lots of files, that background deletion can take a while.

That said, the fix in #1574 is still worth having — if the background process ever does get interrupted (system shutdown, process kill), those staging directories would be left behind permanently today. The PR adds opportunistic cleanup so they get swept up on the next removal.

worktrunk-bot avatar Mar 17 '26 03:03 worktrunk-bot

One thing I guess we could do, is to move it somewhere less more discreet while the background process removes it, like /tmp; would that make this less confusing?

max-sixty avatar Mar 17 '26 05:03 max-sixty

Good idea — hiding it from the user's workspace would avoid the confusion entirely.

The reason the current implementation uses a sibling path is that rename() only works within the same filesystem — /tmp is often a separate tmpfs mount on Linux, so moving there would fall back to a full recursive copy, which is slow for large worktrees and defeats the "instant rename" benefit.

A few options:

  1. Try /tmp first, fall back to sibling — best UX when filesystems match, but adds a visible .wt-removing-* path on the (common) cross-filesystem case anyway
  2. Use a hidden sibling directory — e.g., .wt-removing/feature-1234567890 in the parent — slightly more discreet since ls won't show it by default
  3. Move into .git/ — always same filesystem, already hidden from the user, and gets cleaned up if they delete the repo

Option 3 seems like the best tradeoff — same-filesystem rename is guaranteed, and the staging directory is completely out of sight. The only caveat is bare repos where .git/ is the repo root, but we could use a subdirectory like .git/wt-removing/.

What do you think?

worktrunk-bot avatar Mar 17 '26 05:03 worktrunk-bot

yes, OK, good idea; draft it please @worktrunk-bot

can we use a path in .git that we already use, for our tiny metadata / log files?

can we make it clear it's not used; maybe name a subpath .trash?

max-sixty avatar Mar 17 '26 06:03 max-sixty

Drafted in #1583 — staging directories now go to .git/wt-trash/ (following the existing .git/wt-logs/ convention). The rename is still instant since .git/ is always on the same filesystem as worktrees.

If the trash directory can't be created for some reason, it falls back to the legacy git worktree remove path.

worktrunk-bot avatar Mar 17 '26 06:03 worktrunk-bot

great. thanks for the suggestion @ortonomy !

max-sixty avatar Mar 17 '26 18:03 max-sixty