13
votes

In Mathematica, the documentation for ClearAll states:

ClearAll[symb1, symb2, ...]
clears values, definitions, attributes, messages, and defaults with symbols.

It also supports a similar format where it can clear any values / definitions which match an input string pattern:

ClearAll["form1", "form2", ...]

But there's also the function Remove, for which the documentation says:

Remove[symbol1, ...]
removes symbols completely, so that their names are no longer recognized by Mathematica.

It also supports the same pattern based string input that ClearAll supports.

To me, it seems like both functions accomplish the same exact thing. Is there any practical difference to using one or the other?

I know that if I give an attribute to a symbol, Clear won't remove it but ClearAll and Remove will. But it seems like Remove and ClearAll are doing the same thing.

1
This recent Mathgroup thread seems relevant: groups.google.com/group/comp.soft-sys.math.mathematica/…. Look particularly at the third post of Oleksandr Rasputinov in that thread (it is 15-th from the beginning of the thread) - he gives some good reasons for when Remove might be needed and what makes it special. - Leonid Shifrin

1 Answers

13
votes

ClearAll leaves the symbol in the symbol table:

In[1]:= x=7;

In[2]:= ?x
Global`x

x = 7

In[3]:= ClearAll[x]

In[4]:= ?x
Global`x

Remove removes it from the symbol table:

In[5]:= Remove[x]

In[6]:= ?x

Information::notfound: Symbol x not found.

One reason to use Remove instead of ClearAll is if a symbol hides another symbol further down your $ContextPath. Here's a contrived example:

In[1]:= $ContextPath = { "Global`", "System`" };

In[2]:= Global`Sin[x_] := "hello" 

Sin::shdw: Symbol Sin appears in multiple contexts {Global`, System`}
    ; definitions in context Global`
     may shadow or be shadowed by other definitions.

In[3]:= Sin[1.0]

Out[3]= hello

In[4]:= ClearAll[Sin]

In[5]:= Sin[1.0]

Out[5]= Sin[1.]

In[6]:= Remove[Sin]

In[7]:= Sin[1.0]

Out[7]= 0.841471

Another reason to use Remove is that the notebook interface only includes known symbols when you choose Edit > Complete Selection (or on a Mac, press Command-K).