29

I have iPython Notebook through Anaconda. I accidentally deleted an important notebook, and I can't seem to find it in trash (I don't think iPy Notebooks go to the trash).

Does anyone know how I can recover the notebook? I am using Mac OS X.

Thanks!

billbert
  • 413
  • 1
  • 5
  • 8
  • If you're running OSX, you may have Time Machine running and be able to recover it from the Time Machine backup – iayork Oct 27 '16 at 01:17
  • 1
    On Windows, found my files in Recycle Bin. Hit right click and restore. – Abhi Jun 02 '21 at 21:39

12 Answers12

30

This is bit of additional info on the answer by Thuener,

I did the following to recover my deleted .ipynb file.

  1. The cache is in ~/.cache/chromium/Default/Cache/ (I use chromium)
  2. used grep in binary search mode, grep -a 'import math' (replace search string by a keyword specific in your code)
  3. Edit the binary file in vim (it doesn't open in gedit)
    • The python ipynb should file start with '{ "cells":' and
    • ends with '"nbformat": 4, "nbformat_minor": 2}'
    • remove everything outside these start and end points
  4. Rename the file as .ipynb, open it in your jupyter-notebook, it works.
Vivek-Ananth
  • 494
  • 4
  • 4
  • You are awesome. Thanks a lot. Ideally, "jupyter" people should have solved for it. But awesome method. Thanks again This should get accepted as the answer – TheRajVJain Sep 07 '17 at 09:34
  • 2
    Under Windows you can find the cahe of Chrome in C:\Users\USERNAME\AppData\Local\Google\Chrome\User Data\Default\Cache – maxstrobel Jan 16 '20 at 15:28
  • I was trying to find my deleted notebook in Microsoft edge cache. `C:\Users\USER_NAME\AppData\Local\Microsoft\Edge\User Data\Default\Cache` folder contained only `Cache_Data` folder, but no file. That folder contained hudreds of files named `f_xxxxxx`m, where `xxxxxx` seem to be some hex value. I wrote small python script to search 'nbformat' in all files in that folder. None contained the string `nbformat`. – MsA Jul 16 '23 at 19:34
24

The "delete" functionality now sends the file to OS trash rather than permanently deleting it, see this PR: https://github.com/jupyter/notebook/pull/1968. So you can just open your Trash (wherever that is on your system) and restore it.

exp1orer
  • 11,481
  • 7
  • 38
  • 51
  • Which version? This doesn't seem to be true for jupyter 4.4.0. – virtualxtc Aug 27 '18 at 20:27
  • 1
    Looks like it was added in 5.3.0 https://jupyter-notebook.readthedocs.io/en/stable/changelog.html#release-5-3-0 – exp1orer Aug 30 '18 at 19:55
  • 3
    Yes, I've been meaning to come back and update: turns out I was running a version of `notebook` higher than 5.3, but because my files are saved on a different drive than my home directory, **`Send2Trash` breaks and silently deletes the file rather than trashing it. This is bad** because the confirm delete message was removed because the process isn't suppose to be destructive (but now silently is). – virtualxtc Aug 30 '18 at 20:48
  • Oh, that's unfortunate! I'd suggest opening an issue on Github. – exp1orer Sep 05 '18 at 13:19
  • Great! Could find it in the trash. – Rene B. Nov 02 '18 at 21:10
18

I think the easiest way (until developers handle this issue) to retrieve your Ipython history is to write them all into an empty file.

You need to check by the date you created your last script. Obviously, it is going to be the last part of your Ipython history.

To write your Ipython history into a file:

%history -g -f anyfilename
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
yagmur
  • 183
  • 1
  • 5
13

On linux:

I did the same error and I finally found the deleted file in the trash /home/$USER/.local/share/Trash/files

10

If you deleted it through the OS (rm file.ipynb) then you can probably get it from ~/.ipython_checkpoints/ However, if you deleted it from the browser menu option, it is gone (by design!).

See discussion here: https://github.com/jupyter/notebook/issues/405

Doug Blank
  • 2,031
  • 18
  • 36
  • 1
    The checkpoint is gone by design, but deleting it directly rather than sending it to Trash is by not-got-round-to-that-yet. – Thomas K Aug 10 '16 at 23:30
3

If you use PyCharm, you can do the following.

  1. Open the Local History view.

  2. Select the version you want to roll back to.

  3. On the context menu of the selection, choose Revert.

Worked for me!

Source: here

Community
  • 1
  • 1
3

For the unlucky ones like me, that delete some files on JuliaBox(jupyter for julia), there is a solution. I successifly recovery all my deleted files.

The browsers strore cache information about the pages you visit. You have to find your cache browser folder (in ubuntu with crhome was ~/.cache/google-chrome/Default/Cache) and grep for some text of your notebook in the binarys. Then, cut the text part of the file that is correspond to your ipynb.

https://groups.google.com/forum/#!searchin/julia-box/delete%7Csort:relevance/julia-box/Rt9LG9RldrU/3s_vVSrivJEJ

Thuener
  • 1,359
  • 1
  • 13
  • 13
1

If you're using windows, it sends it to the recycle bin, thankfully. Clearly, it's a good idea to make checkpoints.

user3023715
  • 1,539
  • 2
  • 11
  • 12
1

As long as your Kernel is active, the code of each executed cell is stored in input history list. This will come in handy when you accidentally deleted a cell and want to retrieve its content.

_ih[-10:] *# code of the 10 most recently run cells (Even if those cells are deleted now)*
DesiKeki
  • 656
  • 8
  • 9
1

If you are running on Jupyterlab on linux like me. What I did is went into command prompt and went to my trash folder.

Trash directories on linux are typically

/home/$USER/.local/share/Trash

or

If you deleted something as root (e.g. deleted a file using Nautilus invoked via gksu), it is at

/root/.local/share/Trash

I ended up changing directories to /home/$USER/.local/share/Trash/files and my deleted notebook was there! depending on how you access your backend you could also try /home/jupyter/.local/share/Trash/

ps If you are having issues changing directories from Trash to files due to permissions dont forget to become root:

sudo -i

then after sudo -i, go up with:

cd ..

and then

cd home/jupyter/.local/share/Trash
cd files

Best of luck,

Yev Guyduy
  • 1,371
  • 12
  • 13
0

Sadly my file was neither in the checkpoints directory, nor chromium's cache. Fortunately, I had an ext4 formatted file system and was able to recover my file using extundelete:

  1. Figure out the drive your missing deleted file was stored on:

    df /your/deleted/file/diretory/

  2. Switch to a folder located on another you have write access to:

    cd /your/alternate/location/

  3. It is proffered to run extundlete on an unmounted partition. Thus, if your deleted file wasn't stored on the same drive as your operating system, it's recommended you unmount the partition of the deleted file (though you may want to ensure extundlete is already installed before proceeding):

    sudo umount /dev/sdax where sdax is the partition returned by your df command earlier

  4. Use extundelete to restore your file:

    sudo extundelete --restore-file /your/deleted/file/diretory/delted.file /dev/sdax

  5. If successful your recovered file will be located at:

    /your/alternate/location/your/deleted/file/diretory/delted.file

virtualxtc
  • 390
  • 4
  • 21
0

I had the very problem and I ended up solving it this way. It might be the case for some of the folks.

Pedram
  • 2,421
  • 4
  • 31
  • 49