16
$\begingroup$

What is the difference between Remove and ClearAll?

I thought that "not being recognized by Mathematica", which is the effect of Remove, meant that there would be no "values, definitions, attributes, messages, and defaults associated with symbols", which is the effect of ClearAll. That naive equivalence is wrong as shown by the different output out of these two lines:

b := a; Remove[a]; b
b := a; ClearAll[a]; b

Which side effects of Remove are not shared by ClearAll (and viceversa)?

$\endgroup$
6
  • $\begingroup$ Related: How do I clear all user defined symbols?, especially rm-rf's answer. $\endgroup$
    – Artes
    Nov 22, 2013 at 17:57
  • $\begingroup$ It seems that a symbol is "introduced" when it is first used, even if it is only as rhs. "introduction" not being the same as "definition", which I understand as including an OwnValue, DownValue, etc. With this distinction, Remove does everything that ClearAll does AND it also removes the "introduction" of that symbol. Am I missing something? $\endgroup$
    – Hector
    Nov 22, 2013 at 18:12
  • 1
    $\begingroup$ Remove removes the symbol, ClearAll does not. $\endgroup$ Nov 22, 2013 at 18:17
  • 1
    $\begingroup$ Here's a practical difference: f[a]=1. Now f[a] gives 1; ClearAll[a] does nothing in practice, as a had no definitions, attributes, etc. Now try Remove[a] and evaluate f[a] again. $\endgroup$
    – Szabolcs
    Nov 22, 2013 at 18:49
  • 1
    $\begingroup$ Read also this. $\endgroup$ Nov 22, 2013 at 19:03

2 Answers 2

20
$\begingroup$

ClearAll clears all definitions associated with the symbol. However, the symbol remains in the symbol table, so all references to that symbol from other symbols (their definitions) remain fully valid. The symbol can then acquire new rules or other global properties associated with it.

Remove removes the symbol from the symbol table. More precisely, it removes the association between the symbol name and the actual symbol. In any case, the symbol becomes unusable (normally. There are ways to still use it, will mention that below). The same is true for all references to that symbol from other symbols, which silently become invalid. What this means is that by using Remove, one can subtly invalidate the code in ways which will be very hard to track (but see the link below for some suggestions here). For more details on certain interesting properties of Remove-d symbols, I refer to this discussion.

In terms of their usage, the two functions are quite different. ClearAll is a function intended for quite frequent and general use. Remove, however, I view as a much more specialist and lower-level function. In fact, I would advise against using Remove unless you know exactly what you are doing. It does have a number of advanced uses, but normally the only use which comes to mind is to remove an accidental shadow symbol during the interactive work.

$\endgroup$
0
10
$\begingroup$

Besides destroying the symbol itself, the main side effect of Remove that is not shared by ClearAll is what happens to expressions containing a removed symbol.

a = {x, y};
ClearAll[x, y];a
{x, y} 
Remove[y]; a  
{x, Removed[y]}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.