184

Sometimes I rerun a script within the same ipython session and I get bad surprises when variables haven't been cleared. How do I clear all variables? And is it possible to force this somehow every time I invoke the magic command %run?

8 Answers 8

258

%reset seems to clear defined variables.

9
  • 2
    Thanks. Any way to set it automatically before each %run? Apr 8, 2014 at 13:04
  • 3
    You could convert your script into a function to keep the global namespace clean. Feb 10, 2015 at 9:56
  • 50
    Just found %reset -f clears the global namespace without user confirmation
    – WillJones
    Feb 18, 2015 at 12:21
  • 1
    yes reset -f is great, no need to manually confirm every time any more :) Apr 18, 2018 at 7:40
  • 1
    @Victor % is used for ipython magic commands here's %reset ipython.readthedocs.io/en/stable/interactive/…
    – aisbaa
    Aug 8, 2018 at 11:14
70

EDITED after @ErdemKAYA comment.

To erase a variable, use the magic command:

%reset_selective <regular_expression>

The variables that are erased from the namespace are the one matching the given <regular_expression>.

Therefore

%reset_selective -f a 

will erase all the variables containing an a.

Instead, to erase only a and not aa:

In: a, aa = 1, 2
In: %reset_selective -f "^a$"
In: a  # raise NameError
In: aa  # returns 2

see as well %reset_selective? for more examples and https://regexone.com/ for a regex tutorial.

To erase all the variables in the namespace see:

%reset?
1
  • 12
    Otherwise "del a" from python, can be used as well.
    – SeF
    Aug 17, 2016 at 14:12
65

In iPython you can remove a single variable like this:

del x
0
23

I tried

%reset -f

and cleared all the variables and contents without prompt. -f does the force action on the given command without prompting for yes/no.

Wish this helps.. :)

15

Adding the following lines to a new script will clear all variables each time you rerun the script:

from IPython import get_ipython
get_ipython().magic('reset -sf') 

To make life easy, you can add them to your default template.

In Spyder: Tools>Preferences>Editor>Edit template

1
  • I like this as it can also be run from a script.
    – Zargo
    Jul 1, 2019 at 8:50
13

Apart from the methods mentioned earlier. You can also use the command del to remove multiple variables

del variable1,variable2
4

The get_ipython().magic() method raises a DeprecationWarning in ipython 8.1. Here is the new version of Carl's answer

 from IPython import get_ipython
 get_ipython().run_line_magic('reset', '-sf')

Add these lines to a file you are editing. Then at the ipython command prompt you can type,

%run file_you_are_editing.py
1
  • This answer works for people using for loops, multi files and doesn't want to get their modules erased with the %reset command
    – guardian
    Jan 9 at 14:45
0

An quit option in the Console Panel will also clear all variables in variable explorer

*** Note that you will be loosing all the code which you have run in Console Panel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.