How do I clear all user defined symbols?
Maybe this ?
ClearAll["Global`*"]
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.
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:
- Customize keybindings without modifying files in the installation directory
There are other keyboard shortcuts you may find useful. See these for more:
http://web.ift.uib.no/~szhorvat/mmatricks.php
https://mathematica.stackexchange.com/a/5215/5
https://stackoverflow.com/q/4209405/618728
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:
- Is there a way to separate variables between multiple notebooks?