# Why does Clear[] not work on subscripted variables? [duplicate]

Where is the problem?

Subscript[k, 0] := 1;
Subscript[k, 1] := 2;
ClearAll["Global*"]
Subscript[k, 0]
Subscript[k, 1]
Clear[Subscript[k, 0]]
Clear[Subscript[k, 1]]


• As a general rule I recommend that everyone avoid subscripts and use indexed variables instead, e.g., k[0] and k[1]. After assigning values the subscripted form would not appear in the output anyway. Prior to assigning values, if you want the subscripted form displayed, use Format[k[n_]] := Subscript[k, n] to control the subsequent display. Jun 21 at 18:18
• @BobHanlon's advice is good advice. I wish subscripts were easier to deal with--they can be so darn useful sometimes. $vars = Indexed[x, #] & /@ Range[5]$ $Grad[f[vars], vars]$ Jun 21 at 19:07
• Please use a descriptive title in the future. "Where is the problem?" gives no hint about the contents of your question. Jun 22 at 9:40

## 3 Answers

The problem is that your definitions are associated with Subscript not k.

Subscript is in the System context. I suspect that

(* ClearAll["System*"] *)


would be a bad idea.

You could modify the definition of your symbols, so that they are associated with k

k /: Subscript[k, 0] := 1;
k /: Subscript[k, 1] := 2;

{Subscript[k, 0], Subscript[k, 1]}
(* {1, 2} *)


These can then be cleared

ClearAll["Global*"]
{Subscript[k, 0], Subscript[k, 1]}
(* {Subscript[k, 0], Subscript[k, 1]} *)


Less drastically

Clear[k]


also works (don't specify the subscript, because the results isn't a symbol).

• I got it Subscript is in the System context. Thank you. How to show all subscripts in a notebook? Is there any code such as ?Global*  and ?System* 
– RF_1
Jun 21 at 20:13
• ?Subscript will report subscripts - though I think you will need to click to expand the box containing the symbol information. Jun 21 at 20:24

There is a very useful answer here: StackExchange 2017 by Michael E2 (I shouldn't get credit for this answer...)

Subscript[symbol, 1] = 1
Subscript[symbol, 2] = 2


Now you see it.

Subscript[symbol, 1]


Then you clear it by only retaining downvalues that don't depend on symbol

DownValues[Subscript] =
Cases[DownValues[Subscript], dv_ /; FreeQ[dv, symbol]]


Now you don't.

Subscript[symbol, 1]


One more way if you want to clear individual values.

Subscript[k, 0] := 1
Subscript[k, 1] := 2


You can clear one at a time this way:

Subscript[k, 0] =.


which clears the first one but leaves the second one alone.

Subscript[k, 0]
(*Subscript[k, 0]*)

Subscript[k, 1]
(*2*)
`