ClearAll¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
ClearAll[s1, s2, ...]
clears all values, definitions, attributes and messages for the named symbols. ClearAll[{s1, s2, ...}] accepts a list of specs.
Notes
ClearAll has attribute HoldAll; symbols with attribute Protected are not affected.Examples (12)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (3)¶
In[1]:= f[x_] := x^2; SetAttributes[f, Listable]; Attributes[f]
Out[1]= {Listable}
In[2]:= ClearAll[f]; {Attributes[f], DownValues[f]}
Out[2]= {{}, {}}
In[3]:= x = 2; Remove[x]; x
Out[3]= x
Applications (9)¶
A value and an attribute
Both are gone
DownValues and attributes together
In[7]:= h[x_] := x^2; SetAttributes[h, Listable]; ClearAll[h]; {DownValues[h], Attributes[h]}
Out[7]= {{}, {}}
Compare Clear, which keeps the attributes
A list of specs, mixing symbols and strings
Default options survive; Remove deletes them
A Protected symbol is skipped
So built-ins are never wiped
Implementation notes¶
Algorithm. builtin_clear_all (src/core.c) is one of the four
symbol-management heads driven by core_apply_symbol_action, which walks the
argument list and applies a per-symbol action to each spec ā a bare symbol, a
string naming a symbol, or a flat List of such specs (so ClearAll[{a, b}]
works), with the name read out by core_symbol_name_of. The action here is
core_clear_all_one.
For each name, core_clear_all_one first skips any Protected
symbol (which is what shields every builtin), then does the full erase that
distinguishes ClearAll from Clear: symtab_clear_symbol drops the
OwnValues/DownValues, the attribute word is zeroed (bumping the rule epoch so
the evaluation cache is invalidated), and the docstring (usage message) is
freed. The C builtin function pointer, if any, is left intact ā the Protected
guard already keeps ClearAll away from builtins. The head returns Null.
Attributes & limits. ClearAll carries HoldAll | Protected, so the
symbols arrive unevaluated rather than being replaced by their current values.
Non-symbol and non-string specs are ignored without a message. A Protected
user symbol is also skipped silently, and keeps its values until it is
Unprotected. Protected is the only guard (since v0.301), and every protection
can be lifted with Unprotect.
ClearAllandRemoveboth have attributes{HoldAll, Protected}. Both hold their arguments, so they operate on the symbol, not its current value.- Neither affects symbols with the attribute
Protected. This is what preventsRemove/ClearAllfrom ever deleting or wiping a built-in. ClearAll, unlikeClear, also removes attributes and the usage message.- Both return
Null.
Attributes: HoldAll, Protected.
References¶
- Source:
src/core.c - Specification:
docs/spec/builtins/assignment-and-rules.md - Tests:
tests/test_clearall_remove_protect.c - Tests:
tests/test_condition_downvalue.c - Tests:
tests/test_core.c - Tests:
tests/test_evaluate.c
Notes & additional examples¶
Notes¶
ClearAll[s]removess's OwnValues, DownValues, attributes and usage message, returning the symbol to its undefined state. CompareClear[s], which drops only the values. Registered default options are kept.Removedeletes them along with the symbol.- Arguments may be symbols, strings naming symbols, or a flat list of them. The
result is
Null. HoldAll | Protected, so the symbols themselves are cleared, not their values.- A
Protectedsymbol is skipped silently, which is what keepsClearAllfrom wiping a built-in. To clear one of your own protected symbols,Unprotectit first.Protectedis the only guard, and every protection can be lifted. - See also:
Clear,Remove,Unset,ClearAttributes.