17

In MATLAB there is the function clear to delete all current variables. This is very useful if you start something totally new and don't want to get conflicts with earlier calculations. I'm searching something similar for Mathematica now, but I couldn't find anything except of Clear[VAR] which removes only the variable VAR.

gdelfino
  • 11,053
  • 6
  • 44
  • 48
lumbric
  • 7,644
  • 7
  • 42
  • 53

3 Answers3

17

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

ClearAll["Global`*"]

If you're 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 doesn't 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.

abcd
  • 41,765
  • 7
  • 81
  • 98
16

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:

Community
  • 1
  • 1
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
  • 1
    Could you summarize the two methods here? It's not generally nice to simply link to an existing post. If it really is answered by those exactly, then we should close this as a duplicate. – abcd Nov 25 '11 at 02:24
  • @yoda my understanding of correct answering differs from your own. I do not wish to duplicate existing answers, and I am not linking to external sources, but rather StackOverflow itself. Further, I do not believe this is a duplicate question, and should not be closed. Would it be better if I linked to specific answers rather than those questions? If you feel I my understanding requires correction, please explain why. – Mr.Wizard Nov 25 '11 at 05:21
  • 2
    http://meta.stackexchange.com/questions/113437/answering-questions-with-links-to-other-answers-on-stackoverflow/113444#113444 – abcd Nov 25 '11 at 14:30
  • +1 - I too approve of this answer, as this is a very good example (good grammar, formatting, content, and references to support answer) of the kind of answer that StackOverflow likes to receive. Good work! – casperOne Nov 27 '11 at 19:17
13

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`*"]. There's the << Utilities`CleanSlate` package, which automates a little bit more than this. You can read the package docs inside the AddOns\ExtraPackages\Utilities\CleanSlate.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 haven't 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 can't tell you how well it works.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • is it possible then by just Quiting (rebooting) the kernel, that the front end might itself remember something in its own little space, which would not be removed/cleared by rebooting the kernel? I also use this method of reboot the kernel each time when I want to really make sure things are cleaned up. But I always wondered if the front-end might still remember something. When I want to really really make sure, I just restart all of Mathematica itself. – Nasser Nov 25 '11 at 00:26
  • @Nasser To my knowledge, strictly speaking the Front End won't remember anything. However, it may happen that you have a notebook with dynamic content open, and as soon as this content comes into view, it causes the front end to re-launch the kernel and evaluate whatever is defined in the dynamic object to be evaluated. The confusing part is that this only happens when the dynamic things come into view, so it might seem a bit "indeterministic" if you don't know what's happening. I myself don't restart the front end to have a clean session. – Szabolcs Nov 25 '11 at 08:18