6
$\begingroup$

I assume that there is difference between the commands Clear and Remove. But it seems to me that I am not aware of any caveats against their misuse. For instance,

a = 5; a =.; ?a. (* or *) a = 5; Clear[a]; ?a.*
a = 5; Remove[a]; ?a.

The effects are the same and so is the error message:

Information::nomatch: "No symbol matching a. found."

Another curiosity is about the warning difference if a dot is omitted after a,

a = 5; Remove[a]; ?a

Information::notfound: "Symbol a not found. "

$\endgroup$
2

1 Answer 1

10
$\begingroup$

To see the the difference, try:

a = 1;
f[i_] := a + i
f[1]

2

Clear[a]
f[1]
NameQ["a"]

1 + a

True

Remove[a]
f[1]
NameQ["a"]

1 + Removed["a"]

False

As you can see, Remove has a different effect on other definitions that a is inside of. After Removing, a is not allowed to be used anymore because it doesn't exist anymore, even symbolically. You have to recreate a and any function depending on it, because even if you recreate a, f will still be in a broken state:

a = 1;
f[1]

1 + Removed["a"]

Remove is often overkill, but it's sometimes necessary if you need strict control over which symbols exist and which ones don't. And as Goofy remarks in a comment: Remove also deletes the symbol from the auto completion functionality in the FrontEnd, which can be useful to be aware of.

In short summary:

  • Clear deletes most "normal" definitions associated with a symbol, but not Options and Attributes etc.
  • ClearAll deletes everything associated with a symbol, including Options and Attributes (and some other stuff). In normal everyday use of WL, you'll hardly ever need a bigger cannon than ClearAll.
  • Remove deletes the entire existence of a symbol itself.
$\endgroup$
1
  • 5
    $\begingroup$ Another difference: If I remove a symbol, it doesn't show up in autocompletion. I do this after I mistype a built-in command, since the default autocompletion settings seem to favor my mistakes. $\endgroup$
    – Goofy
    Commented May 24 at 15:34

Your Answer

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

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