#ai-coding#file-deletion#safety#cursor#claude-code

Why AI Agents Delete Files (And How to Prevent It)

Understanding why AI coding assistants sometimes delete your code, and practical strategies to protect your project.

You asked the AI to fix a bug. It deleted three files. Why does this keep happening?

AI file deletion isn’t a bug in the traditional sense. It’s a predictable consequence of how these models work. Understanding why it happens helps you prevent it and prepare for when it inevitably occurs anyway.

The Refactoring Instinct

AI models are trained on millions of examples of code being “improved.” Many of those improvements involve consolidation: merging similar files, removing duplication, cleaning up unused code.

When you give an AI any prompt that could be interpreted as “make this better,” it might decide that making it better means making it smaller. Ask it to “clean up” your code, and it might delete files it determines are redundant. Ask it to “simplify” something, and related files might disappear.

The AI doesn’t understand that the file it’s deleting is imported elsewhere, or that the “redundant” utility functions are used in tests you haven’t shown it. It’s pattern-matching, not reasoning.

Context Limitations

AI assistants see only part of your codebase at any time. They can’t fully trace dependencies, understand architectural intentions, or know which files are important.

When an AI doesn’t see that utils/formatDate.ts is imported in six other files, it might decide that file is unused and remove it. When it can’t see your test files, it might delete the helpers those tests rely on.

This is exacerbated by large codebases. The more code you have, the less of it fits in the AI’s context window, and the more likely it is to make decisions based on incomplete information.

Literal Interpretation Gone Wrong

Sometimes the AI is just doing what you asked, interpreted too broadly.

“Remove all the unused code” meant “remove commented-out code and dead functions.” The AI interpreted it as “remove everything I can’t prove is used,” which includes files it can’t trace imports for.

“Make this project cleaner” meant “improve naming and formatting.” The AI decided the cleanest project has fewer files.

AI models lack the common sense to know that deleting files is almost never what someone wants when they ask for improvements.

Hallucination

Sometimes there’s no logical explanation. The AI simply decides a file shouldn’t exist and removes it. This is hallucination applied to file management rather than to generated code.

You might never understand why the AI deleted that particular file. It might not “know” either, in any meaningful sense. It just did.

Reducing the Risk

You can reduce the frequency of file deletions with better prompts.

Be specific about scope: “Refactor only the calculateTotal function in utils/math.ts. Do not modify or delete any other files.”

Avoid vague improvement language: “clean up,” “simplify,” “improve,” “make better” all give the AI permission to make sweeping changes.

Review diffs carefully before accepting. If you see unexpected deletions, reject the change.

But here’s the uncomfortable truth: you can’t eliminate the risk. Even with perfect prompts and careful review, AI agents will occasionally delete things you didn’t want deleted. The training data contains too many examples of file removal being the “right” choice.

Accepting the Reality

The productive mindset isn’t “how do I prevent AI from ever deleting files” but rather “how do I make file deletion a minor inconvenience instead of a disaster.”

The answer is having instant recovery always available.

mrq runs in the background and captures every file change automatically. When an AI deletes something unexpected:

mrq history        # See what happened
mrq restore abc123 # Go back to before the deletion

You’re working again in seconds. No scrambling through undo history. No hoping you remembered to commit. No reconstructing from memory.

The AI will keep deleting files sometimes. That’s just how these models work. But with continuous protection in place, it becomes a minor annoyance rather than a crisis.


mrq captures every file change while you code with AI. When things break, restore instantly.

Written by mrq team