How to recover deleted Jupyter notebook cell?

Jupyter NotebookJupyter

Jupyter Notebook Problem Overview


I use Jupyter notebook. I accidentally deleted a cell, and I can't go step back.

How can I recover the notebook cell?

Jupyter Notebook Solutions


Solution 1 - Jupyter Notebook

If you go to "Edit", there's an option for "Undo Delete Cells".

In MacOS

If you are familiar with shortcuts, you can do cmd + shift + p and then type in undo to recover as well. No need to moving your mouse around.

In Windows

First, switch to command mode by pressing ESC, then you can press Z to recover the deleted cells. (Thanks to @nik7 who proposed this answer)

Solution 2 - Jupyter Notebook

First, switch to command mode by pressing ESC, then you can press Z to recover the deleted cells. It works even in Jupyter Lab which doesn't have "Undo Delete Cells" option under "Edit" tab.

Solution 3 - Jupyter Notebook

If the notebook kernel is still running and you executed the cell, you can find the content of all cells, including the deleted ones in _ih. For example, the last five cells:

_ih[-5:]

Solution 4 - Jupyter Notebook

If you need to undo something deleted inside a cell, press:
CTRL/CMD + Z

If you need to recover an entire deleted cell hit:
ESC + Z.
Especially this second tip might be useful if you delete a lot of cells by mistake.

Solution 5 - Jupyter Notebook

if you've already restarted your kernel then %history or _ih[:n] would be empty. You can hope for a recent checkpoint to revert to. You can do this with File > Revert to Checkpoint.

If you want to be sure what's in the checkpoint before you revert - in your working directory (the one your notebook lives in) you'll have a hidden folder that stores your checkpoints. cd .ipynb_checkpoints will get you there. You can look inside the file and see if your lost code is there.

This has just saved me. Found this in an answer here

Solution 6 - Jupyter Notebook

First, do "ESC" hot key, then you can press Z to recover the deleted cells.

Jupyter Lab work well.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHISIView Question on Stackoverflow
Solution 1 - Jupyter NotebookTYZView Answer on Stackoverflow
Solution 2 - Jupyter NotebookRishabh AgrahariView Answer on Stackoverflow
Solution 3 - Jupyter NotebookMike MüllerView Answer on Stackoverflow
Solution 4 - Jupyter NotebookHadis BagherpourView Answer on Stackoverflow
Solution 5 - Jupyter NotebookPolina SklyarevskyView Answer on Stackoverflow
Solution 6 - Jupyter NotebookJ.LucasView Answer on Stackoverflow