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
?

- 20,030
- 7
- 43
- 238

- 3,988
- 3
- 23
- 29
8 Answers
%reset
seems to clear defined variables.

- 9,867
- 6
- 33
- 48
-
2Thanks. Any way to set it automatically before each %run? – grasshopper Apr 08 '14 at 13:04
-
Don't know, but you could run python script from shell terminal. – aisbaa Apr 08 '14 at 14:00
-
hey grasshopper did you find out how to do this? So annoying! – WillJones Feb 04 '15 at 18:33
-
3You could convert your script into a function to keep the global namespace clean. – Robert Pollak Feb 10 '15 at 09:56
-
53Just found %reset -f clears the global namespace without user confirmation – WillJones Feb 18 '15 at 12:21
-
1yes reset -f is great, no need to manually confirm every time any more :) – user3723247 Apr 18 '18 at 07:40
-
@aisbaa what is the "%" sign called in this case ? – Aug 07 '18 at 09:09
-
1@Victor `%` is used for ipython magic commands here's `%reset` https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-reset – aisbaa Aug 08 '18 at 11:14
-
With %reset, I got errors like ERROR: root: Invalid alias: The name clear can't be aliased because it is another magic command. I assume I can ignore these types of errors. – Rom Sep 22 '22 at 18:04
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?

- 3,864
- 2
- 28
- 41
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

- 151
- 1
- 3
Apart from the methods mentioned earlier. You can also use the command del to remove multiple variables
del variable1,variable2

- 703
- 8
- 16
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

- 51
- 1
- 2
-
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 09 '23 at 14:45
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

- 1
- 5