# Why julia has no clear(variable)?

**URL:** <https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 18, 2017, 11:15pm UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541 "2017-10-18T23:15:49Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 18, 2017, 11:15pm UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/1 "2017-10-18T23:15:49Z")

</div>

Here is a quote from the documentation

> Julia does not have an analog of MATLAB’s clear function; once a name is defined in a Julia session (technically, in module Main), it is always present.
> 
> If memory usage is your concern, you can always replace objects with ones that consume less memory. For example, if A is a gigabyte-sized array that you no longer need, you can free the memory with A = 0. The memory will be released the next time the garbage collector runs; you can force this to happen with gc().

Is there a reason for this? The proposed workaround, `A = 0`, has the problem that if I attempt to use the variable `A` at a later point, there will be no error, which can be unsafe when my intended behaviour wast to clear the variable to prevent its future use.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 18, 2017, 11:48pm UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/2 "2017-10-18T23:48:28Z")

</div>

It cannot be a function and it’ll have very different semantics from the matlab function. It is easily implementable in local scope though in most cases what you can use is simply `let`. Ones that cannot be represented by `let` scope of a variable is likely very hard for human to understand too.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 19, 2017, 12:17am UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/3 "2017-10-19T00:17:32Z")

</div>

As I understand it, `let` is preventive, not therapeutic. If you already have a declared variable that you want to get rid off, `let` will not help you.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 19, 2017, 12:19am UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/4 "2017-10-19T00:19:01Z")

</div>

Well, what I mean is that you only assign that variable in the let scope and the end of the `let` will clear the variable for you. You can do

```julia
local A
let A
   A = ...
end
A # error()

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 19, 2017, 12:32am UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/5 "2017-10-19T00:32:21Z")

</div>

But there is nothing fundamental that prevents adding a `clear` _keyword_ to the language someday, right? Is there a reason not to?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 19, 2017, 1:17am UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/6 "2017-10-19T01:17:06Z")

</div>

It’ll likely be a macro instead of a keyword. The biggest issue I can think of is that it can’t operate on global variables (const or non-const)

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [October 19, 2017, 2:08am UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/7 "2017-10-19T02:08:05Z")

</div>

Maybe try:

```julia
A = nothing

```

Most methods won’t be defined on `A` which is of type `Void`, so you should get an error.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 19, 2017, 7:37am UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/8 "2017-10-19T07:37:01Z")

</div>

I like this. I don’t know why I did not think of it before. I think the Julia docs (the quote is from here: [https://docs.julialang.org/en/stable/manual/faq/](https://docs.julialang.org/en/stable/manual/faq/)) should use this example, instead of `A = 0`.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 19, 2017, 7:43am UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/9 "2017-10-19T07:43:08Z")

</div>

[https://github.com/JuliaLang/julia/issues/24205](https://github.com/JuliaLang/julia/issues/24205)

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [October 19, 2017, 9:00am UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/10 "2017-10-19T09:00:44Z")

</div>

Wouldn’t it be better to update the documentation to use a `let` block so that the variable is cleared when the block ends? (Maybe keeping the `A = nothing` hack as a not-recommended-but-also-works option.)

Some methods are defined for `Void` input, so there’s no guarantee that `A = nothing` will always give errors when `A` is used incorrectly. But more importantly, letting the variable fall out of scope will also give information to linters, so the editor might flag the incorrect use of `A` as soon as the line is written, without ever running the code.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 19, 2017, 9:38am UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/11 "2017-10-19T09:38:21Z")

</div>

I agree that perhaps the `let` block is a better approach. But the issue is when you _already_ have a variable that you defined in global scope. I use the REPL for quick and dirty prototyping, which means I usually have a lot of global variables lying around. I don’t have time to think about structuring my code using `let` blocks in this context.

So the issue here is what to do with a variable that you defined in the global scope already. Not whether it _would_ have been better to define it with a `let` block. You can’t change the past 😛

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [October 19, 2017, 1:24pm UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/12 "2017-10-19T13:24:51Z")

</div>

> [@yuyichao](#):
>
> It cannot be a function

I think this is really what needs to be in the FAQ, along with the explanation of the quoted statement. I’ve accepted that there isn’t a way to delete objects, but I think people would stop asking if it was more clear why its a hard/impossible problem.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 19, 2017, 1:32pm UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/13 "2017-10-19T13:32:36Z")

</div>

“It cannot be a function” does not explain why the language was not designed with a _keyword_ to delete bindings (like `del` in Python).

---

<div class="post-metadata">

**Author:** ![foobar\_lv](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@foobar\_lv](https://discourse.julialang.org/u/foobar_lv)\
**Post date:** [October 19, 2017, 4:19pm UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/14 "2017-10-19T16:19:05Z")

</div>

Re cossio: del in python does not delete objects, it deletes symbols. In the python3 REPL:

x = [1,2,3]  
y= x  
del x #equivalent to del globals()[‘x’]  
print(y)  
[1, 2, 3]

This is a very different thing. C does not remove symbols from the scope (but the preprocessor does, i.e. you can remove macro definitions; it is an interpreted language, after all, and not compiled).

Python has no keyword or function for deleting objects (freeing their memory). This is not so bad in python, since it is reference-counted; hence, unless you created a cycle, the object will be free’d immediately once it goes out of scope (normally by leaving the scope, possibly by del).

You could legitimately ask why julia does not expose the global symbol table to you, i.e. does not provide an analogue of  
del globals()[‘x’]  
on the REPL (removing entries from dictionaries is easy in julia). I find that I don’t particularly care, I have no need to access the global symbol table programatically.

One possible answer: Defining the semantics of the language in view of such changes would be really hard. In python this is not an issue, because python functions are essentially interpreted (bindings are call-time), whereas many bindings in julia are compile-time or parse-time.

A more interesting question would be how to manually free the memory pointed at by an object reference (expecting memory corruption / segfault / pwnage if other references survive). You would do this to relieve gc pressure while avoiding the cost of actually garbage collecting.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [October 19, 2017, 4:31pm UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/15 "2017-10-19T16:31:52Z")

</div>

I am sorry, I meant deleting a variable binding. Note that deleting symbols is precisely what I want. It’s not about memory, it’s about removing a symbol from scope for safety, so that I don’t inadvertently misuse it later.

---

<div class="post-metadata">

**Author:** ![Kirby\_Zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirby_zhang/32/36204_2.png) [@Kirby\_Zhang](https://discourse.julialang.org/u/Kirby_Zhang)\
**Post date:** [May 30, 2022, 6:13am UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/16 "2022-05-30T06:13:04Z")

</div>

I think ‘let’ is a good structure. just takes some getting used to, much better than the hacks people have gotten used to under Python.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 30, 2022, 7:09am UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/17 "2022-05-30T07:09:07Z")

</div>

> [@foobar\_lv](#):
>
> You could legitimately ask why julia does not expose the global symbol table to you, i.e. does not provide an analogue of  
> del globals()[‘x’]  
> on the REPL (removing entries from dictionaries is easy in julia).

Now that this thread has been revived anyway: _is_ it possible to do this? I agree with this:

> [@e3c6](#):
>
> Note that deleting symbols is precisely what I want. It’s not about memory, it’s about removing a symbol from scope

The `let` solution is just too inconvenient, I’m never going to be bothered with that.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 30, 2022, 2:05pm UTC](https://discourse.julialang.org/t/why-julia-has-no-clear-variable/6541/18 "2022-05-30T14:05:51Z")

</div>

I agree. Deleting a symbol might be useful. In fact that could probably be linked to trigger a garbage collection pass to reclaim the memory (if the symbol was the last reference).
