In this tutorial, we'll see how to recover deleted cells and restore previous sessions of Jupyter Notebook. We'll also see how to restore an accidentally deleted notebook.
Step 1: Jupyter Notebook restore deleted cell
There are several ways to recover deleted cells. Let's start with the easiest one using the menu:
- Go to Notebook menu
- Undo Delete Cells
Note: For JupyterLab this option is missing. In this case you need to use: Undo Cell Operation. The short cut is Z - you need to press escape before applying it.
Step 2: Jupyter Notebook restore cells with active Kernel
If the Notebook Kernel is still active then you can use commands which are reading the last executed cells from the Kernel. For example reading the last 5 commands can be done by:
Note: it shows even deleted cells
Step 3: Jupyter Notebook view current or full execution history
This step will help even if the Kernel is not active. Jupyter Notebook history is available by command(it can be executed in the notebook):
If you like to save the recovered execution history to a given file like:
notebook_file.ipynb then you can use:
%history -g -f notebook_file.ipynb
-g defines the history scope:
-g- current session
-g- all sessions
This is the last resort in some cases.
Step 4: Jupyter Notebook restore checkpoint/session
Jupyter Notebook Checkpoints are simple version control systems which save sessions. You can find them from the UI:
- Save and Checkpoint - save the current session
- Revert to Checkpoint - restore previous session
- Select Checkpoint
Another way to access them and recover your work is from the file system. Open the Notebook folder and search for:
Then search for the file name of your Notebook. The file pattern is:
Note: How to find your folder:
.ipynb_checkpoints/. You can check the current folder by command like:
executed in the cell.
Step 5: JupyterLab restore deleted Notebook
If you delete a Notebook in JupyterLab or in the browser then they can be found in folder like (Linux; for Windows it goes to Recycle Bin):
Now the file can be restored by move or copy.
On Linux and Mac a command like:
find ~/ | grep notebook_file.ipynb
might help to search for the deleted file.
Step 6: Restore Notebook from the browser cache
One more way to recover deleted Notebook is by next algorithm:
- Find the cache for your browser
- Search for a specific code like:
import pandas as pd
grep -a 'import pandas as pd'
- since the command is really slow you can search for the cache from the last hour by -
grep "^$(date -d '-1 hour' +'%H')" | grep -a 'pandas'
- for the current folder only -
grep -r "^$(date -d '-1 hour' +'%H')" . | grep -r -a 'pandas' .
- Copy the found file
- Edit the copied binary file with nano:
- The final Notebook file should:
- start with
- ends with
"nbformat": 4, "nbformat_minor": 2}
- start with
- Save the file as
- use advanced version control when it's possible
- activate multiple checkpoints
Good luck with the Notebook recovery and restore!