1
$\begingroup$

My Mathematica variable scope is set to Notebook, more about it here: What is my current scope if I change `Inspector>Cell options>Evaluation options>CellContext` to "Notebook"?

Now I want to clear all variables from the scope.

I am getting current scope:

In[61]:= $Context
Out[61]= Notebook$$34$511107`

After I am using this output by manually constructing this command and it works (clear all variables from the scope):

Clear["Notebook$$34$511107`*"]

All above works well. Now I want to do that programatically. I tried this:

Clear[Evaluate["\"" <> $Context <> "*\""]]

also this:

Clear["\"" <> Evaluate[$Context] <> "*\""]

but without any success.

How to fix code above to make it work?

$\endgroup$
  • $\begingroup$ I found universal option to use Clean[] for all cases: Clear["`*"] $\endgroup$ – vasili111 Jul 13 at 2:31
2
$\begingroup$

You need to turn $Context into a string. You also do not need to add the quotation marks, as you are joining strings, so the end result will automatically be a string again.

This should do what you want.

Clear@Evaluate[ToString[$Context] <> "*"]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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