Is there a way to clear everything before starting in Mathematica?

Is there a way to clear everything before starting in Mathematica?

You can use ClearAll to clear the variables and their attributes in your Global context (default) like so:

ClearAll[Global`*]

If youre working inside a different context (e.g., notebook specific context or cell group specific context), you can do

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

If you want to remove all symbols from the kernel so that Mathematica doesnt recognize them anymore, you can use Remove[] similar to the above two examples.

Barring these, you can always quit the kernel with Quit[] which will remove all symbols. A fresh kernel will be initiated the next time you evaluate something.

I recommend one of two methods:

1. Keyboard shortcut to Quit[] the kernel

There is a system file KeyEventTranslations.tr that you can edit to customize keyboard shortcuts. I, as others, have added Ctrl+Q to Quit[] the kernel, allowing for a rapid clearing of all sessions variables. For more information on setting this up, see:

2. Give the new Notebook a unique context

In Mathematica, the current $Context defines what Context unqualified symbol names belong to. By giving a new Notebook 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 clear everything before starting in Mathematica?

I just realized that you might not know that unlike MATLAB, Mathematica is designed to run as two separate processes: the Front End is the user interface, and lets you work with notebooks. The Kernel does the computations. You can quit the kernel without affecting the front end, or even start more than one kernel for different notebooks, or start a kernel on a remote computer and use it with a local front end.


I believe that the only reliable way to clean everything is to Quit the kernel and re-start it (which is automatic). There are just too many things that can get modified apart from user variables/functions (including In/Out, loaded packages, system caches, etc.). So if you need a truly fresh start, I recommend Quit.

For a soft reset, @yoda already mentioned ClearAll[Global`*]. Theres the << Utilities`CleanSlate` package, which automates a little bit more than this. You can read the package docs inside the AddOnsExtraPackagesUtilitiesCleanSlate.m file.

In short, CleanSlate[] will attempt to take you back to the kernel state when the package was loaded. ClearInOut[] will clear In and Out to save memory.

I havent used this package in years (except for the ClearInOut[] functionality), as the Mathematica kernel starts up quickly on modern computers, so I just use Quit. So I cant tell you how well it works.

Leave a Reply

Your email address will not be published.