Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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)?

share|improve this question
    
Related: How do I clear all user defined symbols?, especially rm-rf's answer. –  Artes Nov 22 '13 at 17:57
    
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? –  Hector Nov 22 '13 at 18:12
1  
Remove removes the symbol, ClearAll does not. –  Andreas Lauschke Nov 22 '13 at 18:17
1  
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. –  Szabolcs Nov 22 '13 at 18:49
1  
Read also this. –  Leonid Shifrin Nov 22 '13 at 19:03
show 1 more comment

2 Answers

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.

share|improve this answer
    
+1 "accidental shadow symbol" –  Hector Nov 22 '13 at 20:37
add comment

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]}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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