WOLFRAM

Clear[s1,s2,]

clears values and definitions for the symbols si.

Clear[patt1,patt2,]

clears values and definitions for all symbols whose names textually match any of the arbitrary string patterns patti.

Clear[{spec1,spec2,}]

clears values and definitions for any symbols that are equal to or whose names match any of the speci.

Details

  • Clear does not clear attributes, defaults, options or messages associated with symbols. »
  • The pattern patt can be given as a string with metacharacters, as StringExpression[] or as RegularExpression["regex"]. »
  • Clear allows abbreviated string patterns containing the following metacharacters:
  • *zero or more characters
    @one or more characters, excluding uppercase letters
  • Clear["context`*"] clears all symbols in a particular context. »
  • Clear["`*"] clears all symbols in the current context. »
  • Clear does not affect symbols with the attribute Protected. »
  • Clear has attribute HoldAll. »

Examples

open allclose all

Basic Examples  (2)Summary of the most common use cases

Clear a variable:

The variable no longer has a value:

Out[3]=3

Define values for a symbol:

Out[2]=2

Clear values:

Out[4]=4

Scope  (12)Survey of the scope of standard use cases

Symbol Inputs  (7)

Clear values of variables (ownvalues):

Out[2]=2
Out[4]=4

Clear functions (downvalues):

Out[2]=2
Out[4]=4

Clear upvalues:

Out[2]=2
Out[4]=4

Clear subvalues:

Out[2]=2
Out[4]=4

Clear formatting:

Out[2]=2
Out[4]=4

Clear several symbols:

Out[2]=2
Out[4]=4

Use a combination of symbols and symbol names:

Only x3 remains defined:

Out[3]=3

Using Patterns  (5)

Specify symbols to clear as string patterns:

Out[3]=3

Clear all symbols in the current context:

Out[3]=3

Clear all symbols in a given context:

Out[3]=3

Clear all 2-character symbols in the current context using StringExpression:

The symbols x1 and x2 were cleared, but y remains unaffected:

Out[3]=3

Clear all 3-character symbols in the current context using RegularExpression:

Out[3]=3

Applications  (2)Sample problems that can be solved with this function

Clear any old definitions before making new ones:

Out[4]=4

Unprotect and clear all symbols in a package, to allow it to be read twice:

Properties & Relations  (6)Properties of the function, and connections to other functions

Clear[pattern] clears the same symbols as Clear/@Names[pattern]:

Out[3]=3
Out[6]=6

Use Unset (=.) to clear definitions with a particular left-hand side:

Out[3]=3

Clear all definitions:

Out[5]=5

Clear does not remove attributes, defaults or options:

Out[3]=3

Use ClearAll to clear everything:

Out[5]=5

Clear does not remove messages:

Use ClearAll to clear messages:

Clear removes all definitions but leaves the symbol intact:

Out[3]=3

Remove removes the symbol completely:

Out[5]=5

Clear has the attribute HoldAll:

Out[1]=1

This clears symbol itself:

Out[4]=4

This clears its value:

Out[7]=7

Possible Issues  (2)Common pitfalls and unexpected behavior

Protected symbols cannot be cleared:

Use Unprotect to clear definitions of protected symbols:

When using a pattern without a context mark, all matching symbols on $ContextPath are cleared:

The symbol xy was cleared, along with attempts to clear several system symbols:

Out[2]=2

Use a pattern with an explicit context mark to avoid potentially matching symbols from system or other contexts:

Wolfram Research (1988), Clear, Wolfram Language function, https://reference.wolfram.com/language/ref/Clear.html (updated 2022).
Wolfram Research (1988), Clear, Wolfram Language function, https://reference.wolfram.com/language/ref/Clear.html (updated 2022).

Text

Wolfram Research (1988), Clear, Wolfram Language function, https://reference.wolfram.com/language/ref/Clear.html (updated 2022).

Wolfram Research (1988), Clear, Wolfram Language function, https://reference.wolfram.com/language/ref/Clear.html (updated 2022).

CMS

Wolfram Language. 1988. "Clear." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/Clear.html.

Wolfram Language. 1988. "Clear." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/Clear.html.

APA

Wolfram Language. (1988). Clear. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Clear.html

Wolfram Language. (1988). Clear. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Clear.html

BibTeX

@misc{reference.wolfram_2025_clear, author="Wolfram Research", title="{Clear}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/Clear.html}", note=[Accessed: 28-April-2025 ]}

@misc{reference.wolfram_2025_clear, author="Wolfram Research", title="{Clear}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/Clear.html}", note=[Accessed: 28-April-2025 ]}

BibLaTeX

@online{reference.wolfram_2025_clear, organization={Wolfram Research}, title={Clear}, year={2022}, url={https://reference.wolfram.com/language/ref/Clear.html}, note=[Accessed: 28-April-2025 ]}

@online{reference.wolfram_2025_clear, organization={Wolfram Research}, title={Clear}, year={2022}, url={https://reference.wolfram.com/language/ref/Clear.html}, note=[Accessed: 28-April-2025 ]}