AI Deleted My Code: How to Recover
Your AI coding assistant just deleted important files. Here's how to recover them and prevent it from happening again.
It happens fast. You’re iterating with Cursor or Claude Code, things are going well, and then the AI decides your codebase needs “cleaning up.” Suddenly, files are gone. Maybe it consolidated what it thought were duplicates. Maybe it removed “unused” code that was actually imported elsewhere. Maybe it just hallucinated that a file shouldn’t exist.
If you’re reading this, it probably just happened to you. Let’s get your code back, and then make sure it never happens again.
First: Don’t Close Anything
Your editor likely still has deleted files in memory. Every tab you close is potential recovery lost. Keep everything open while you work through this.
Check Your Editor’s Undo History
Start with the obvious. If the deleted file’s tab is still open, try Ctrl+Z (or Cmd+Z on Mac) repeatedly. Sometimes the file content is recoverable even after the file itself was deleted from disk.
This doesn’t always work, especially for files the AI deleted without opening, but it’s worth trying first.
Use Local History
Both VS Code and Cursor maintain a local history of file changes that most developers don’t know about. Press Ctrl+Shift+P (or Cmd+Shift+P), type “Local History: Find Entry to Restore”, and browse for your deleted files.
This works even for files that were deleted, as long as they were edited at some point. It’s saved me more times than I’d like to admit.
Check Your AI Tool’s Recovery Features
If you’re using Cursor, look for “Checkpoint created” messages in the Composer panel. Each prompt creates a checkpoint you can restore to. Click “Restore” next to the one from before the deletion.
For Claude Code, press Esc twice or type /rewind to open the checkpoint menu. You can restore just the code, just the conversation, or both.
The limitation with these is that they’re tied to conversation context. If you’ve done a lot of work since the deletion, or if the deletion happened through Tab completions rather than Composer, you might not have a useful checkpoint.
Fall Back to Git
If you committed before the AI made its changes, Git is your friend:
git status # See what's been deleted
git checkout . # Restore everything to the last commit
If you didn’t commit… well, that’s the problem, isn’t it? Git can only restore what you remembered to save.
When Nothing Works
If none of the above recovered your files, check your system backups. Time Machine on Mac, File History on Windows, or cloud sync services like Dropbox often keep recent versions. It’s a long shot, but sometimes it works.
If you’ve exhausted all options and your code is truly gone, you’ve learned an expensive lesson about working with AI.
Preventing This Next Time
The fundamental problem is that AI agents are unpredictable. You can write better prompts, you can be more careful about reviewing diffs, but you can’t eliminate the risk entirely. AI will occasionally delete things you didn’t want deleted.
The solution is having recovery baked in, not bolted on.
Git works, but it requires discipline. You have to commit before every AI interaction, which breaks your flow when you’re iterating quickly. And the one time you forget is the time the AI goes haywire.
This is why we built mrq. It watches your project directory and captures every file change automatically. No commits, no decisions, no interruptions. You just run mrq watch and forget about it.
When an AI deletes something:
mrq history # See recent snapshots
mrq restore abc123 # Go back to any point
You’re back to exactly where you were, even if you never committed. Even if the deletion happened three AI interactions ago. Every change is captured, every state is recoverable.
The reality of AI coding is that things break unexpectedly. The question is whether you’re prepared when they do.
Related Reading
- How to Recover Deleted Files from Cursor and Claude Code - Detailed recovery steps for specific AI tools
- Why AI Agents Delete Files (And How to Prevent It) - Understanding why this happens
- Best Practices for AI Pair Programming - Prevent problems before they start
mrq captures every file change automatically while you code with AI. Set it once, recover instantly when things break.
Written by mrq team