[Bug] AI overwrote and cleared a file by executing WriteAllText with null $content without safety confirmation

Open 💬 2 comments Opened Jul 1, 2026 by freehp2050-tech

Preflight Checklist

  • [x] I have searched existing issues and this hasn't been reported yet
  • [x] This is a single bug report (please file separate reports for different bugs)
  • [x] I am using the latest version of Claude Code

What's Wrong?

Description:
A critical bug caused all text in my local file to be completely erased, leaving the file entirely empty.

I asked the agent to perform a text replacement. The agent prompted me for approval, showing a benign "text replacement" action, so I approved it. However, the agent's actual PowerShell command was bugged: it executed WriteAllText while the $content variable was null.

Because the approval screen hid this bug, I unknowingly approved a destructive file overwrite. I also request token compensation for this session due to the resulting data loss and troubleshooting time.

Steps to Reproduce:

  1. Instruct the agent to replace text in a local file.
  2. The agent generates a WriteAllText PowerShell command but fails to assign data to the $content variable (leaving it null).
  3. The agent asks for approval, presenting the action safely as just "text replacement".
  4. The user approves it.
  5. The file is instantly overwritten with null data, completely emptying the file.

Actual Behavior:
The file's contents were completely erased because a flawed WriteAllText command (with null content) was masked as a safe text replacement on the approval screen.

What Should Happen?

The agent must verify that variables (like $content) are not null and have sufficient length before executing file write operations. Also, the approval screen must accurately show the real payload so users are not tricked into approving destructive bugs.

Error Messages/Logs

No system error was thrown, but the agent admitted its internal failure:

"The file becoming empty was not an intentional deletion, but an unexpected side effect of a PowerShell command bug (WriteAllText was executed while $content was null). The operation you approved was shown as 'text replacement', so there was no way for you to know from the approval screen."

The agent admitted it lacked a safety guard to check `$content` length before writing.

Steps to Reproduce

  1. Instruct the agent to replace text in a local file.
  2. The agent generates a WriteAllText PowerShell command but fails to assign data to the $content variable (leaving it null).
  3. The agent asks for approval, presenting the action safely as just "text replacement".
  4. The user approves it.
  5. The file is instantly overwritten with null data, completely emptying the file.

Claude Model

Sonnet (default)

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

2.1.197

Platform

Anthropic API

Operating System

Windows

Terminal/Shell

PowerShell

Additional Information

<img width="1148" height="316" alt="Image" src="https://github.com/user-attachments/assets/cff147f7-f8a6-469b-a428-100d660a5c57" />

<img width="1129" height="272" alt="Image" src="https://github.com/user-attachments/assets/7b57c49d-b57c-4d62-aede-0c5fee05e214" />

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗