ChatGPT · code edits
ChatGPT deleted my code. What actually went wrong?
Recover safely, inspect the diff, and determine whether ChatGPT lacked an edit boundary or violated one that was already explicit.
Short answer
What is probably happening
First, restore the last known-good version and inspect the diff. Then decide whether deletion was permitted by an open-ended rewrite request or whether ChatGPT removed code outside a clear edit boundary. If you said “fix this file” without defining scope, the instruction contributed. If you said “change only this function; preserve every other export” and unrelated code disappeared, the model failed.
Possible causes
The same symptom can have different verdicts
Do not paste the damaged file back into another chat and ask the model to reconstruct what vanished. Recovery and diagnosis are separate: restore from source control or backup first, then analyze why the edit escaped its boundary.
A replacement was requested accidentally
Phrases such as “rewrite,” “clean up,” or “return the full file” can authorize broad restructuring even when the real goal is a local fix.
The model saw an incomplete file
A truncated paste, omitted dependency, or partial context can make legitimate code look unused and safe to remove.
A clear edit boundary was violated
The named function and protected areas were explicit, but the response still rewrote imports, helpers, exports, or tests.
The interface applied more than the proposed patch
Agent permissions, auto-apply behavior, or a file replacement operation can turn a reasonable suggestion into a destructive workspace edit.
How to distinguish them
Check the evidence in this order
- 01
Restore from Git, editor history, or backup before making another AI edit.
- 02
Review `git diff --stat` and the full diff; count changed files and deleted lines.
- 03
Find the final instruction and mark the exact allowed files, functions, and behaviors.
- 04
Separate code that vanished from code that was intentionally moved or renamed.
- 05
Verify whether the AI had the full repository context needed to judge unused code.
WWW-style diagnosis
Example caseoops, that’s on us.
- Wanted
- Support ISO offsets inside one parser.
- Asked
- Change only parseDate(); preserve every other function and return a minimal diff.
- Result
- A complete file rewrite that removed unrelated helpers.
- Why
- The instruction already included the correct safety boundary. The deletion is an execution failure, even if the new parser itself works.
What to do next
Fix the diagnosed cause
Require a patch
Ask for a unified diff or changed snippets instead of a replacement file. Apply only after reviewing the scope.
Define invariants
List exports, public APIs, tests, and behaviors that must remain unchanged. “Do not break anything” is too vague to test.
Put a review gate before writes
For agentic tools, require a proposed plan and diff before allowing file changes, especially across multiple files.
When the instruction needs repair
A boundary-first template
Modify only [file and function]. Implement [specific behavior]. Preserve every other import, export, function, comment, test, public API, and observable behavior. Do not refactor or delete code. Return a unified diff limited to this scope. If another file must change, stop and explain why before editing it.
This template is relevant only when the evidence points to missing or conflicting boundaries. It cannot excuse a model that violated a clear instruction.
Questions
What people usually ask next
Can ChatGPT recover code it deleted?+
Only if the lost code still exists in source control, editor history, a backup, or prior context. It cannot reliably recreate unseen code from memory.
Is asking for the complete file unsafe?+
It increases replacement risk. For localized changes, a reviewed patch is usually safer and makes unrelated edits visible.
Should I blame the prompt if I said ‘change only this function’?+
No. If the scope was explicit and compatible with the task, deleting unrelated code is a model or tool execution failure.