How to clear variables in ipython?

PythonMemoryIpython

Python Problem Overview


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?

Thanks

Python Solutions


Solution 1 - Python

%reset seems to clear defined variables.

Solution 2 - Python

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?

Solution 3 - Python

In iPython you can remove a single variable like this:

del x

Solution 4 - Python

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.. :)

Solution 5 - Python

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

Solution 6 - Python

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

del variable1,variable2

Solution 7 - Python

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

Solution 8 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongrasshopperView Question on Stackoverflow
Solution 1 - PythonaisbaaView Answer on Stackoverflow
Solution 2 - PythonSeFView Answer on Stackoverflow
Solution 3 - PythonJoopView Answer on Stackoverflow
Solution 4 - PythonBabu K.M.View Answer on Stackoverflow
Solution 5 - PythonCarlView Answer on Stackoverflow
Solution 6 - PythonDevarshi MandalView Answer on Stackoverflow
Solution 7 - PythonSirishView Answer on Stackoverflow
Solution 8 - PythonchubbView Answer on Stackoverflow