# How to clear workspace variables？

**URL:** <https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 1, 2020, 5:39pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095 "2020-08-01T17:39:45Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![dty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dty/32/16885_2.png) [@dty](https://discourse.julialang.org/u/dty)\
**Post date:** [August 1, 2020, 5:39pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/1 "2020-08-01T17:39:46Z")

</div>

I’ve installed it revise.jl How can I use this bag? How can I clear the previous working variables after modifying the program to see the new variable values

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [August 1, 2020, 6:43pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/2 "2020-08-01T18:43:04Z")

</div>

Your question is a bit hard to understand.

Revise.jl basically allows you to change function definitions without having to restart the REPL. You don’t need to clear your previous definitions.

Similarly, when you run your code, it will overwrite previous variable values. You don’t need to clear those either before running the code.

If this does not answer your question, please provide an example of what you are trying to accomplish.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [August 1, 2020, 6:43pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/3 "2020-08-01T18:43:37Z")

</div>

You can’t. Julia doesn’t have a way of clearing the “workspace”, even with Revise.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 1, 2020, 7:02pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/4 "2020-08-01T19:02:20Z")

</div>

Just run your function again with the new parameter values.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [September 19, 2020, 5:55am UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/5 "2020-09-19T05:55:23Z")

</div>

> [@tomerarnon](#):
>
> You can’t. Julia doesn’t have a way of clearing the “workspace”, even with Revise.

I would love to get this functionality. It seems like it keeps coming up, from the google results for it as well…

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 19, 2020, 1:40pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/6 "2020-09-19T13:40:54Z")

</div>

> [@TheLateKronos](#):
>
> It seems like it keeps coming up, from the google results for it as well…

I think this happens because both people come from `R` and `Mathlab` to Julia, and both have this feature. I am not sure if people coming from other languages miss it. You have an use case that is not covered by assigning `nothing` to the binding?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [September 19, 2020, 1:43pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/7 "2020-09-19T13:43:34Z")

</div>

You might find Pluto.jl useful. It promises that the objects defined in your workspace are exactly the objects in your notebook.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 19, 2020, 1:45pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/8 "2020-09-19T13:45:12Z")

</div>

Good point. It would be interesting to know how they achieve this. The only way I know is re-running Julia.

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 19, 2020, 1:49pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/9 "2020-09-19T13:49:35Z")

</div>

It is mentioned in their juliacon talk. If I remember correctly it’s done via something like internal modules where a new module is created in the background to get a new namespace. But this could be entirely wrong

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 19, 2020, 1:53pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/10 "2020-09-19T13:53:44Z")

</div>

I am also a R refugee but the only time this bothers me is when developing new structs that cannot be overwritten. Ironically I feel like julia would benefit more than R from `rm(list=ls())` to clear the workspace without restarting.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 19, 2020, 2:04pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/11 "2020-09-19T14:04:07Z")

</div>

> [@danielw2904](#):
>
> new structs that cannot be overwritten

This is known to be an issue, but unfortunately handling it is not easy. See

> <https://github.com/timholy/Revise.jl/issues/18>
>
> Since \`struct\`s cannot be redefined, attempting to do so currently gives a \`fail…ure to evaluate changes\` warning.
> 
> I wonder if this can be worked around by triggering a reload of the entire module in this case.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 19, 2020, 2:13pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/12 "2020-09-19T14:13:52Z")

</div>

Pluto also allows this.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [September 19, 2020, 8:11pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/13 "2020-09-19T20:11:44Z")

</div>

> [@Henrique\_Becker](#):
>
> You have an use case that is not covered by assigning `nothing` to the binding?

Not as in “this can not be done without clearing the workspace”, but as in it messes with my workflow. I have a julia script that I use as a calculator, mainly in my mechanics and thermodynamics class. I seperate tasks with “##”, and as you can imagine, I sometimes define something like theta a bunch of times,in multiple tasks. It would be nice to know that if I forget to define a value for the current task, the value from the previous one is not used without warning. It is more for peace of mind than functionality… I know that is not the most common use for julia, but it just seems like it should be possible, and I would like it ☀

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [September 19, 2020, 8:51pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/14 "2020-09-19T20:51:33Z")

</div>

This is what I do in my `jupyter` notebook:

```julia
x, y, z = let
    ...
    inner_x, inner_y, inner_z
end

other_results = let x = deepcopy(x), y = deepcopy(y), z = deepcopy(z)
    ...
    inner_other_results
end

```

So, each cell has a `let` block, and if I use some `global` variable I use the `let` line to assign deepcopies of these values to new bindings (so nothing outside the `let` scope is ever changed), if I think the cell should introduce some global values, I let them escape the `let` block (e.g. `x`, `y`, `z`, `other_returns`).

I do not exactly see you use case because:

1. If you would manually delete some names, then you can (probably) set them to `nothing` with the same effort (ok, in some cases this may not work, but then `NaN`, `missing`, or `struct NothingReally; end` will probably work).
2. If you wanna go nuclear (something like `rm(list=ls())`), then you can just restart Julia (this may be too slow), or actually use some macro that finds all defined bindings and assign `nothing` to them.
3. If you want a compromise, you may want `Pluto.jl` or use `let` scopes (or even module scopes) to avoid leaking bindings between one ‘task’ and the next.

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 20, 2020, 12:16am UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/15 "2020-09-20T00:16:18Z")

</div>

Its not really a big issue either. More to say that for variables its not really an issue to not ‘delete’ them.

---

<div class="post-metadata">

**Author:** ![xuanquansheng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xuanquansheng/32/32400_2.png) [@xuanquansheng](https://discourse.julialang.org/u/xuanquansheng)\
**Post date:** [September 16, 2022, 1:07pm UTC](https://discourse.julialang.org/t/how-to-clear-workspace-variables/44095/16 "2022-09-16T13:07:18Z")

</div>

Yes, It’s so inconvenient!
