15

To clear the console we can use the following command -

import subprocess as sp
tmp = sp.call('cls',shell=True)

However, to remove a variable from memory, we often rely upon -

  • using del command
  • removing variable manually by using the drop-down menu in variable explorer

But both of them are variable specific and hence time-consuming. So is there any general command (like clear under MATLAB) to remove a variable from memory and thereafter from Spyder's Variable Explorer.

sync11
  • 1,224
  • 2
  • 10
  • 23
  • 2
    If you want to restart the ipython console, you can use `%reset`, it will clear all your variables, but maybe you are looking to deleted only one variable ? – Coding thermodynamist Aug 24 '17 at 05:54
  • 1
    Well, I'm looking to remove all variables at once within spyder python scripts. – sync11 Aug 24 '17 at 05:58
  • 1
    Possible duplicate of [How to clear variables in ipython?](https://stackoverflow.com/questions/22934204/how-to-clear-variables-in-ipython) – Jose Dzireh Chong Aug 24 '17 at 07:35
  • 1
    @JoseDzirehChong the given page doesn't list a solution for normal py scripts rather gives a way for IPython scripts. @Nathan, we can't issue `%reset` command directly. So, what steps are required to make it usable for python scripts? – sync11 Aug 24 '17 at 07:52
  • @Vivek, you can automatically clear your namespace in Spyder by going to the menu `Run > Configuration per file > General settings` and selecting the option called `Clear all variables before execution`. – Carlos Cordoba Aug 24 '17 at 19:54

5 Answers5

27

Go to the IPython console in the Spyder IDE and type %reset. It will prompt you to enter (y/n) as the variables once deleted cannot be retrieved. Type 'y' and hit enter. That's it.

NKoder
  • 471
  • 5
  • 6
21

In Spyder, Do following steps
Run
Configuration per file...
Clear all variables before execution [Select Checkbox]

This actually clears variables from previous run of the file. Hope it helps.

enter image description here

Karan Kaw
  • 510
  • 1
  • 7
  • 15
  • In recent versions of Spyder, the option is directly under `Run` in the configuration dialog. – mins Sep 01 '22 at 14:00
3

Surfing on the web, I found a hack to solve the annoying problem of clearing the variable explorer every time you want to execute again a script:

def clear_all():
    """Clears all the variables from the workspace of the spyder application."""
    gl = globals().copy()
    for var in gl:
        if var[0] == '_': continue
        if 'func' in str(globals()[var]): continue
        if 'module' in str(globals()[var]): continue

        del globals()[var]
if __name__ == "__main__":
    clear_all()
    # insert here your code

Basically, it consists of executing the function clear_all() just before everything else. It is writing by yourself the same Matlab's function. Here the link to the git issue where the solution was proposed.

Leos313
  • 5,152
  • 6
  • 40
  • 69
3

As explained in the answer provided by Karan Kaw, there is a setting to delete all the variables, regardless of the script you are workin on.

Do the following (from the drop-downs bar)

> Tools > Preferences > Run

and check the Remove all the variables before execution checkbox in the General Setting section. In doing this I use Spyder 3.3.4.

This completion may be in order if you want all the scripts you work on to be executed with the preventive deletion of all the variables. Hope it helps

matte
  • 97
  • 8
2

For Spyder v. > 4.0, it is possible to remove the variables just by clicking the "remove all variables" icon at the variable explorer:

enter image description here

Javier TG
  • 465
  • 2
  • 7
  • 15