You restore a class in IntelliJ IDEA through the Local History feature, which keeps automatic snapshots of your project files and lets you roll a deleted or damaged class back to an earlier version. Right-click the folder that contained the class, choose Local History → Show History, find the snapshot from before the file disappeared or was changed, and use Revert to bring it back. That is the core answer, but it needs one qualification: Local History only works if the class existed in your IntelliJ project and the relevant snapshots have not yet been overwritten or purged. If the class was never opened in IntelliJ, or the history window has expired, you need a different recovery route such as version control, your IDE's file-system trash behavior, or an external backup.

The rest of this article explains how Local History actually behaves, how to use it step by step, why people often can't find the deleted class where they expect it, and what to do when Local History comes up empty.

What "restore a class" can mean in IntelliJ

The question is ambiguous in a way that matters, because IntelliJ offers several different kinds of restoration and they are not interchangeable:

Restoring a deleted file, including a .java or .kt class file that was removed from disk or from the project. Reverting a modified class to an earlier version after bad edits. Re-adding a class that is missing from the project model — for example, after a module or source root was detached, so the file exists but IntelliJ no longer sees it as part of the project. Recovering from a refactor mistake, such as an accidental rename, move, or inline operation.

The first two are Local History problems. The third is usually a project-structure problem, not a lost file. The fourth is often solved by undo or by Local History, depending on how the refactor was applied. Keep these apart, because applying a file-recovery method to a configuration problem will not help, and vice versa.

How Local History works and why it is the primary tool

IntelliJ IDEA maintains a per-project Local History that is independent of Git or any other version control system. As you edit files, the IDE records changes and periodically captures snapshots. It also records structural events such as file creation, deletion, and certain refactorings. This means that even in a project with no VCS at all, a deleted class usually leaves a trace.

Two properties of Local History are essential to understand:

It is scoped to the project, not the whole disk. Local History lives in the IDE's system directory for that project. It tracks files that are part of the project (and, in practice, files you have opened or that IntelliJ has indexed). A class that was never part of the project is not in Local History.

It is finite. IntelliJ retains a limited number of changes per file and purges older entries over time. The exact retention depends on settings and on how much you edit; there is no guarantee that a snapshot from weeks ago still exists. This is the most common reason a restore attempt fails: the deletion happened long ago, or the file was edited heavily afterward, and the old snapshot has been rotated out.

Local History is not a backup system. It is a convenience for recent mistakes. Treating it as a safety net for important work is a mistake; version control is the real safety net.

Restoring a deleted class with Local History, step by step

The procedure is short, but the exact target you right-click determines what you see.

1: Identify the package folder. In the Project tool window, navigate to the directory that held the class — for example src/main/java/com/example/service. If the whole directory was deleted, go one level up to its parent. 2: Open Local History on that folder. Right-click the folder and select Local History → Show History. You can do the same on the project root if you are unsure where the class lived; the history view for a directory lists changes to files under it. 3: Scan the revision list. The left pane shows revisions in reverse chronological order, labeled with an action such as Created, Deleted, Changed, or a refactoring name, plus a timestamp. Look for the entry where the file was deleted, or the last entry before it. 4: Inspect before restoring. Selecting a revision shows the file tree for that snapshot in the right pane. Open the class to confirm it is the version you want. This step prevents the classic mistake of reverting a folder to a state that also undoes unrelated, wanted changes. 5: Revert selectively. Right-click the specific file in the snapshot and choose Revert. If you are restoring a single deleted file, this is safer than reverting the whole directory. Reverting the directory restores every file in that snapshot to its old state, which can clobber later work. 6: Verify. Reopen the class, check that it compiles, and confirm it sits in the correct package. If the restore reintroduced an outdated version, you can still edit forward from there.

A useful refinement: if you only want one file back, use the history view for that file if it still appears somewhere, or use the directory history and revert just the one entry. IntelliJ lets you revert individual files from a directory snapshot, which is exactly the control you want.

Reverting a modified class rather than a deleted one

If the class still exists but has been damaged — a bad merge, an accidental overwrite, a refactor gone wrong — the same Local History view works, but you select the file itself rather than its folder. Right-click the file, choose Local History → Show History, pick the last good revision, and revert it.

Undo (Ctrl+Z / Cmd+Z) reverses the most recent editing operations within the current session and is the fastest fix for a just-made mistake, including some refactorings. It is limited to the current session's undo stack and does not survive an IDE restart.

The Local Changes and shelf features apply only when the project is under version control. If the class was committed and then deleted, or deleted but not yet committed, your VCS (Git, SVN, etc.) is a better and more durable source than Local History. In Git, a deleted-but-uncommitted file can be restored with a checkout from the index; a committed deletion can be recovered from history. IntelliJ surfaces these operations through the Git menu and the Commit tool window, but the underlying recovery is Git's, not the IDE's.

When the class is "missing" but not actually deleted

A frequent variant of the question is that the class file is still on disk but IntelliJ shows it in red, stops compiling it, or does not list it at all. In that case you are not restoring a file; you are repairing the project model. Common causes:

The source root was unmarked. Check File → Project Structure → Modules and confirm the folder containing the class is marked as a Sources root. A class outside any source root is not compiled or indexed as project code. The module was removed or the .iml file was lost. Re-import the module or re-add the content root. The file was excluded. Look for an excluded folder under Project Structure → Modules → Sources. Indexing is stale or corrupt. File → Invalidate Caches / Restart rebuilds the indexes; this is the standard remedy when classes exist but the IDE behaves as if they do not.

These are configuration fixes, and no amount of Local History reverting will solve them. Diagnose first: if the file is visible in your OS file manager, the problem is almost certainly in the project model, not in the file's existence.

Refactoring accidents: rename, move, and inline

A renamed class looks deleted because the old name is gone. Local History records the rename as a labeled event, so you can often revert to the pre-rename snapshot. But the cleaner fix depends on the operation:

Rename or move: if you catch it immediately, Undo reverses the refactor. If not, revert the affected files or directory from Local History to the state before the refactor. Be aware that a refactor may have touched many files; reverting only the class file can leave references inconsistent, so review the whole change set. Inline: inlining a method or class removes the original declaration. Reverting from Local History restores it, but any call sites that were rewritten during the inline may also need reverting to keep the code consistent. This is a case where reverting the entire affected directory snapshot is often the right move, precisely because consistency matters more than preserving later edits.

The general rule: restore at the smallest scope that fixes the problem, but when a change touched multiple files as a unit, restore the unit.

What Local History cannot do

Being clear about the limits prevents wasted effort:

It does not recover files that were never part of the project. It does not retain history indefinitely; old snapshots are purged. It does not survive deletion of the IDE's system directory, a clean reinstall that wipes settings, or moving the project to another machine without migrating the IDE configuration. It is not a substitute for version control, and it does not protect against disk failure.

If Local History shows nothing useful, the realistic options are version control history, your operating system's trash or recycle bin (a file deleted through the OS or a file manager may be there), any backup or sync service covering the project folder, or — in the worst case — reconstructing the class from compiled .class files via a decompiler. Decompilation recovers structure and logic but not comments or exact formatting, and it should be a last resort.

Practical habits that make restoration reliable

The reason this question is common is that people discover Local History only after a loss. A few habits change that:

Put the project under Git (or another VCS) from day one and commit frequently. This makes recovery deterministic and durable, and it is the only method that scales beyond recent mistakes. Know where Local History lives and avoid deleting the IDE's system directory casually; it holds this history. Use the IDE's own delete and refactor operations rather than deleting files in the OS file manager, so the operations are recorded cleanly. When something disappears, check Local History before doing anything drastic such as re-importing the project or invalidating caches, which can complicate recovery.

Restoring a class in IntelliJ is usually a thirty-second operation through Local History, provided you act while the relevant snapshot still exists and you target the right scope — the file for a single damaged class, the containing folder or module for a deletion or a multi-file refactor. When the history is gone, version control and backups are what remain, which is why the durable answer to "how can a class be restored" is ultimately "keep it under version control and treat Local History as the quick fix for recent mistakes."