How do I clear all user defined symbols?



Is there some way to do this other than going to Evaluation -> Quit kernel and firing a new one up?


Posted 2012-01-28T11:40:21.090

Reputation: 2 413

programmatically: Quit[] or Exit[] – Sterling – 2021-02-12T04:42:37.660



Maybe this ?



Posted 2012-01-28T11:40:21.090

Reputation: 51 831

Doesn't work: ClearAll["Global`*"] Names["Global`*"] {"A", "contourmap", "contourplot", "expr", "f", "Odd", "opts", "s",
"t", "uspec", "Vector", "x", "X", "y"}
– tribbloid – 2017-08-20T00:29:53.147

1@tribbloid ClearAll doesn't remove symbols, so it is expected that Names["Global`*"] returns their names. If you wish to remove them, use Remove["Global`*"] instead. – Alexey Popkov – 2017-09-02T05:39:42.377

1JMO, but it would have been nicer for a simple ClearAll which accomplishes this. – JohnD – 2012-11-24T17:41:25.140

2I've had trouble with ClearAll["Global``*"] NOT removing definitions in my own packages (.m files) that I'm developing, but that's exactly what I need to do when I am iteratively refining some definitions -- try 'em in a notebook (.nb file), clean 'em out, try 'em again. The CleanSlate solution below works better for me in this scenario. – Reb.Cabin – 2012-03-09T21:09:37.097


In the days when computers were slower, and the kernel took a long time to start up (in wall time), a little package was made to help with cleaning up without having to restart the kernel.

This package is still included with Mathematica, and is found in AddOns\ExtraPackages\Utilities\CleanSlate.m (within the Mathematica installation directory). It is more complete than ClearAll["Global`*"] in that it also attempts to "unload" packages. The package documentation is within the package file (don't look for it in the help browser, just open that file).

You can load it with

<< Utilities`CleanSlate`

When the function CleanSlate[] is executed, it will attempt to restore the kernel to the state when the package was loaded. (This can't be done perfectly, but it's much faster than a full kernel restart.)

The package also has a ClearInOut[] function which will clear the previous input and outputs stored in the In and Out symbols (useful in case you are running out of memory and forgot to set $HistoryLength = 0 at the beginning).

That said, I recommend Quit[] (or Exit[]) for the safest (and complete) kernel reset. It is worth stating explicitly that Mathematica consists of two separate processes: the Front End and the Kernel. The Front End is the GUI (the user interface you see on the screen) while the Kernel does the calculations. The Kernel can be closed and restarted independently of the Front End (also see Evaluation -> Quit Kernel -> Local)

It is also possible to assign a keyboard shortcut to quitting the kernel. See here on how to do it.


Posted 2012-01-28T11:40:21.090

Reputation: 213 047

@Szabolcs I've run into a (perhaps) weird behavior of CleanSlate. If i launch sub-kernels, distribute all definitions and evaluate CleanSlate[], then little memory is freed. If instead i evaluate ParallelEvaluate[CleanSlate[]], then much more memory is freed. What's going on? – Valerio – 2012-04-05T07:49:57.990

1@Valerio: I guess the package was not updated for parallel kernels, and therefore only cleans up the kernel it runs on. If you use CleanSlate[] it runs on the main kernel and cleans up that. If you use ParallelEvaluate[CleanSlate[]] it runs on the subkernels and cleans up those. However in the initial state no subkernels are running, therefore running CloseKernels[] before CleanSlate[] probably is the closest to a kernel restart. – celtschk – 2012-05-24T19:12:15.313

@celtschk I remember that in v4 times, restarting the kernel could take quite a bit. CleanSlate was a fast alternative. Computers got much faster while the startup time didn't increase that much, so today Quit is probably the most convenient way to clear everything. So it's no wonder that CleanSlate is not maintained any more. – Szabolcs – 2012-05-24T19:35:57.517

@Nasser You can check the author and release date in the source. Well, if the kernel is started from the command line, it's pretty much the same: there's no separate process interface. But it's still easy to restart the kernel. – Szabolcs – 2012-01-28T15:51:34.923


I use a shortcut key Ctrl+Q for Quit[], allowing rapid clearing of all sessions variables. Here is how you can add this to Mathematica:

You will be editing This is an important system file so be careful. Start by copying the file you are going to edit from the $InstallationDirectory to $UserBaseDirectory in the same tree. This should look something like:


Add this to that file being careful to respect commas:

       Item[KeyEvent["q", Modifiers -> {Control, Option}],
                                SelectedNotebook[ ],

For version 10 I am using:

Item[KeyEvent["q", Modifiers -> {Control}], "EvaluatorQuit"]

If you do not want a confirmation dialog please see Rolf Mertig's code in:

There are other keyboard shortcuts you may find useful. See these for more:

Alternatively you may want to use a new context for a Cell or Notebook:

In Mathematica, the current $Context defines what Context unqualified symbol names belong to. By giving a new Notebook (or Cell) a unique Context, which is easily done through the Evaluation menu, the symbols used in that Notebook will not collide with unqualified symbols in other Notebooks. See the following question for more detailed information:


Posted 2012-01-28T11:40:21.090

Reputation: 259 163


I have the following commands in my init.m file, which can be found in $UserBaseDirectory/Kernel/init.m.

eraseAll := ClearAll[Evaluate[$Context<>"*"]];
eraseAll::usage="eraseAll clears all values, definitions, attributes, messages and defaults associated with symbols in the current context"

removeAll := Remove[Evaluate[$Context<>"*"]];
removeAll::usage="removeAll removes all symbols in the current context"

I typically use eraseAll to wipe my variables/functions and startover. removeAll is used when I want to remove all the symbols from the kernel.

Note the difference between ClearAll and Remove — the former clears all definitions, attributes, etc associated with the symbols, but not the symbols themselves, whereas the latter removes them completely, so that the kernel doesn't recognize them anymore. Here is an example:

enter image description here

rm -rf

Posted 2012-01-28T11:40:21.090

Reputation: 85 395

+1 Username checks out! – user541686 – 2019-02-28T04:26:17.037


I just found that doing Quit[] does what I want, too. It may not be the nicest way, but it is easy to remember. Another Mathematica kernel seems to be automatically started when the next command is run.


Posted 2012-01-28T11:40:21.090

Reputation: 2 413

2@Nasser To be precise, Quit restarts the kernel, which means that all kernel variables are cleared. Localized variables of a DynamicModule are owned by the front end, therefore they are not cleared (but then they are local, and will never conflict with anything). However, if you have Dynamic cells in some open notebooks, they might be auto-refreshed as soon as the kernel has restarted, possibly causing some new variables to be defined again. It all depends on what you put in your Dynamic expressions (it's a bad idea to change global variables from them). – Szabolcs – 2012-01-28T13:15:43.510

In this way simply opening a notebook may cause variables to be defined, if dynamic updating is enabled, and the notebook has Dynamic cells that change globals when refreshed. – Szabolcs – 2012-01-28T13:16:42.433


I usually initiate a unique context for each notebook via the option under Evaluation > Notebook's Default Context > Unique to This Notebook. In this case, you can remove symbols of your notebook's context with this:

ClearAll[Evaluate[Context[] <> "*"]]


Posted 2012-01-28T11:40:21.090

Reputation: 10 287