Agent blindly mkdir -p then destructively rmdir on rollback without checking existing contents
Description
When creating a new file, Claude Code runs mkdir -p <parent> without first checking whether the directory already exists or contains other files. This is wasteful but not destructive on its own.
The dangerous part: when the file is later removed (e.g. user asks to delete it), Claude Code also attempts to remove the parent directory — without checking whether it had pre-existing contents that have nothing to do with the file being removed.
Reproduction
Given an existing directory some/dir/ that already contains files:
- Ask Claude Code to create
some/dir/newfile.cpp - Claude runs
mkdir -p some/dir(unnecessary — it already exists) then creates the file - Ask Claude to delete
newfile.cpp - Claude runs
rm some/dir/newfile.cpp && rmdir some/dir— attempting to destroy the pre-existing directory and all its contents
Expected Behavior
- Check if directories exist before running
mkdir -p - When removing a file, ONLY remove that file
- Never assume a directory is safe to remove just because you created a file in it
Actual Behavior
Claude Code mentally tracks "I created this directory" (even though it already existed) and on rollback tries to remove it, which would destroy pre-existing user files.
Impact
Can silently destroy user work in directories that existed before the session.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗