Is there some way to do this other than going to Evaluation -> Quit kernel
and firing a new one up?
6 Answers
Maybe this ?
ClearAll["Global`*"]
-
2$\begingroup$ I'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. TheCleanSlate
solution below works better for me in this scenario. $\endgroup$ Commented Mar 9, 2012 at 21:09 -
2$\begingroup$ JMO, but it would have been nicer for a simple
ClearAll
which accomplishes this. $\endgroup$– JohnDCommented Nov 24, 2012 at 17:41 -
$\begingroup$ Doesn't work: ClearAll["Global
*"] Names["Global
*"] {"A", "contourmap", "contourplot", "expr", "f", "Odd", "opts", "s", \ "t", "uspec", "Vector", "x", "X", "y"} $\endgroup$ Commented Aug 20, 2017 at 0:29 -
1$\begingroup$ @tribbloid
ClearAll
doesn't remove symbols, so it is expected thatNames["Global`*"]
returns their names. If you wish to remove them, useRemove["Global`*"]
instead. $\endgroup$ Commented Sep 2, 2017 at 5:39
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.
-
$\begingroup$ @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. $\endgroup$– SzabolcsCommented Jan 28, 2012 at 15:51
-
$\begingroup$ @Szabolcs I've run into a (perhaps) weird behavior of
CleanSlate
. If i launch sub-kernels, distribute all definitions and evaluateCleanSlate[]
, then little memory is freed. If instead i evaluateParallelEvaluate[CleanSlate[]]
, then much more memory is freed. What's going on? $\endgroup$– ValerioCommented Apr 5, 2012 at 7:49 -
1$\begingroup$ @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 useParallelEvaluate[CleanSlate[]]
it runs on the subkernels and cleans up those. However in the initial state no subkernels are running, therefore runningCloseKernels[]
beforeCleanSlate[]
probably is the closest to a kernel restart. $\endgroup$– celtschkCommented May 24, 2012 at 19:12 -
$\begingroup$ @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 todayQuit
is probably the most convenient way to clear everything. So it's no wonder thatCleanSlate
is not maintained any more. $\endgroup$– SzabolcsCommented May 24, 2012 at 19:35
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 KeyEventTranslations.tr
. 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:
\AppData\Roaming\Mathematica\SystemFiles\FrontEnd\TextResources\Windows\KeyEventTranslations.tr
Add this to that file being careful to respect commas:
Item[KeyEvent["q", Modifiers -> {Control, Option}],
FrontEndExecute[
FrontEndToken[
SelectedNotebook[ ],
"EvaluatorQuit",
Automatic
]
]
]
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:
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:
-
1
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.
-
2$\begingroup$ @Nasser To be precise,
Quit
restarts the kernel, which means that all kernel variables are cleared. Localized variables of aDynamicModule
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). $\endgroup$– SzabolcsCommented Jan 28, 2012 at 13:15 -
$\begingroup$ 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. $\endgroup$– SzabolcsCommented Jan 28, 2012 at 13:16
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[] <> "*"]]
Quit[]
orExit[]
$\endgroup$