Clear cached images in Jupyter Notebook
Asked Answered
N

6

10

I have a jupyter notebook that creates a local cache of images. I would like to clear the cache before I run a particular cell. Does anyone know how to do that? I tried:

import gc
gc.collect()

But it had no effect. I also tried clear all the cookies and cache in chrome, but that did work either.

Navelwort answered 30/3, 2018 at 3:35 Comment(1)
Don't say "the cache". gc is Python's garbage-collection for in-memory objects, only - it's not even a 'cache' in the sense of 'persists in files'. Whereas jupyter is running under your web browser which has an entirely different cache (or 'temporary internet files' as some OSs call them). You wouldn't expect changing the oil on your car to change the radio presets, either...Kraigkrait
U
5

Try the following in a new cell of your Jupyter Notebook:

%reset -f

The above will clear all variables without User Confirmation. If you want explicit user confirmation, just drop the -f.

%reset_selective -f varName

The above will delete particular variable.


In case the Python version you are using is greater than 3.3, you may also try (I haven't used it!):

%store -d someVar  # Remove particular variable and its value from storage

%store -z          # Remove all variables from storage

For more info, check this doc.

Unicuspid answered 26/1, 2021 at 11:20 Comment(1)
I usually keep this cell as the first cell in my notebook and run it before executing my code. This saves from restarting the kernel each time!Unicuspid
S
1

I realise this isn't a great (bordering on terrible) answer - in short, close and re-open the browser.

I've been struggling with this myself today, and even clearing the browser cache did not seem to work. The only way I've found refresh images is to:

  1. Save your notebook
  2. Halt and close the session
  3. Close the browser
  4. When you re-open the notebook - the new images will appear

... I even tried deleting the referenced image file. But the browser still held onto it.

Savonarola answered 8/11, 2019 at 17:19 Comment(2)
Any luck finding a better solution? I have a similar problem that leads my RAM to be full and I think it's silly that I can't find a solution for that!Reitman
@Reitman - In short, no ... sorry. When my Notebooks start running slow, I've realised the (my) issue is that the Plotly graphs contain ~500K data points, so the browser (memory) is struggling to keep up. So clearing the cell output for the ones I'm currently not using has helped. Albeit, this isn't a cache issue.Savonarola
S
1

This worked for me, when an old image would not be refreshed in a markdown cell in Jupyter-Lab on Chrome:

  1. Give the image path a bad path in your markdown code
  2. Execute that markdown cell so it errors.
  3. Rename the image path correctly.
  4. Voila! It should refresh.
Sidon answered 23/10, 2022 at 20:0 Comment(1)
maybe a browser force reload page (CTRL+F5) could be a working solution ?Swahili
I
0

Python in jupyter is not the same, i think that the automatic garbage collection will work at some point in the code, every step is paused and cached, this makes it very memory intensive to deal with buffers. The simples solution es to add a destructor to the elements and remove de need for a gc within the requires steps.

This example uses empty buffers as a load, if you have enough ram to open all instances at the same time it will be enough to just use del img_buffs if not handle them in chunks.

n_images = 1024
img_buffs = {}
for i in range(n_images):
    img_buffs[i] = (0,) * 150000
for i in range(n_images):
    del img_buffs[i]
Izaak answered 25/1, 2021 at 14:28 Comment(0)
P
0

I was able to define an unset() function / method (similar to that in PHP), except I could pass in the variable / object / class name as a string. This method deletes the target string from Pyton's globals.

I am not sure if this is terrible practice, but it worked for the purpose of deleting things I've defined in a Jupyter notebook.

def unset(varname):
"""
Unsets a global variable from memory. Helpful when we are running code blocks in Jupyter multiple times.
"""
try:
    del globals()[varname]
    print('deleted ' + varname)
except:
    print(varname + ' does not exist')

Here are some examples with a class definition and an instance:

class MyTest:
somevar = 1234

def __init__(self):
    print('MyTest')

myT = MyTest()
unset('MyTest')

This will output:

MyTest

deleted MyTest

You can see if the instance is still defined:

print(myT)

It is, and the output is something like:

<main.MyTest object at 0x107bd54d0>

You can then unset the instance object:

unset('myT')

Output:

deleted myT

Comments and suggestions are welcome!

Portcullis answered 27/2, 2023 at 22:4 Comment(0)
C
0

I had this problem when trying to pull large models(file sizes of more 10 GB) from the repo. After a lot of searching and comparing file sizes, I found out that the files were getting stored under C:\Users<your-username>.cache path.

Car answered 25/7 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.