2
  1. How can I show all currently active variables in MM? I've lost track of which variables I've defined and old variables are corrupting new variables (even variables defined in different notebooks).
  2. How can I delete all currently active variables in MM? Do I have to delete them one by one?

(BTW - I'm new here.) Thank you.

Chris O
  • 91
  • 1

1 Answers1

4

I assume in question #1 you're concerned with variables that you have defined yourself. You were probably defining them in the Global context, so you can see all such variables with this:

Names["Global`*"]

You could try Names[] if you're curious about all of the symbols currently "active" across all contexts (so, this includes all of the System symbols as well).

For #2, you can "delete" to a variety of degrees. The most common is probably ClearAll:

ClearAll["Global`*"]

I suggest that you look up Names and ClearAll in the documentation and follow the threads. You might find several other useful tools.

UPDATE

This is found in the documentation, but just for completeness here...

ClearAll doesn't actually remove the variable from Mathematica's "memory". It simply removes all definitions (OwnValues, DownValues, etc) and attributes associated with the symbol(s). For most use cases, this is sufficient and preferable. To make Mathematica actually "forget" the symbol(s), use Remove.

So, in your question, you said "old variables are corrupting new variables". ClearAll should fix that problem (unless I'm misunderstanding you). But you could go "nuclear" and use Remove. Alternatively the ultimate "nuclear" is to kill and restart the kernel.

lericr
  • 27,668
  • 1
  • 18
  • 64
  • Thanks – but it’s not working the way I expect it to. I created a variable Testvar = x+7, then entered ClearAll["Global`*"] and I still see Testvar defined when I enter Names["Global`*"] – Chris O Jan 28 '23 at 17:00
  • Right. I should have been clearer. I'll update the answer. – lericr Jan 28 '23 at 17:28
  • While Testvar is still in the Global context, whatever definitions you created for it will be cleared. – lericr Jan 28 '23 at 17:35
  • OK – but is it just me or wouldn’t you think there was a much simpler, more elegant way to clear all variables than to restart the kernel? I’m not missing something obvious am I? – Chris O Jan 29 '23 at 15:56
  • Clear, ClearAll, Remove. How are these functions difficult? What kind of thing did you have in mind? – lericr Jan 29 '23 at 16:06