# Clearing a variable?

**URL:** <https://discourse.julialang.org/t/clearing-a-variable/32618>\
**Category:** New to Julia\
**Created:** [December 23, 2019, 11:32am UTC](https://discourse.julialang.org/t/clearing-a-variable/32618 "2019-12-23T11:32:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [December 23, 2019, 11:32am UTC](https://discourse.julialang.org/t/clearing-a-variable/32618/1 "2019-12-23T11:32:04Z")

</div>

I believe this has been asked and answered before; however I’ve gone through many of the related posts and I still can’t find how to do it. In my case, I had an array called T, which after a while I didn’t need. What I wanted to do was to create a function called T instead. I was told that

```
julia> T = nothing

```

would clear it, which it sort of does - at least it clears all memory allocated to it. But if I then try

```
julia> T(n) = n+1

```

say, I receive the error:

```
ERROR: cannot define function T; it already has a value

```

Is there any way of completely removing a user variable from the namespace? Or of writing over it?

I know I could stop and restart Julia, but I have a number of variables and functions I’ve been playing around with (I do a lot of experimenting in the REPL), that I don’t want to have to rewrite in a new session. So for me, clearing or removing a variable would be very useful indeed. Thanks!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 23, 2019, 11:50am UTC](https://discourse.julialang.org/t/clearing-a-variable/32618/2 "2019-12-23T11:50:07Z")

</div>

> [@amca01](#):
>
> Is there any way of completely removing a user variable from the namespace?

No

> [@amca01](#):
>
> Or of writing over it?

No, variable can only be declared `const` at their first definition (and function-defs do this implicitly).

(Also, a variable which was declared `const` cannot be bound to a value of another type.)

Sorry!

---

<div class="post-metadata">

**Author:** ![Sijun](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Sijun](https://discourse.julialang.org/u/Sijun)\
**Post date:** [December 23, 2019, 11:52am UTC](https://discourse.julialang.org/t/clearing-a-variable/32618/3 "2019-12-23T11:52:18Z")

</div>

Instead, you can do this way:

```julia
T = (n)->n+1

```

---

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [December 23, 2019, 2:16pm UTC](https://discourse.julialang.org/t/clearing-a-variable/32618/4 "2019-12-23T14:16:37Z")

</div>

Thank you - well, I guess that means I might have to re-consider how I use Julia. In my current session `varinfo()` lists about 38 user-defined variables and functions. This means that I can’t re-use, as it were, any previously used names for something else. No doubt this is a design choice made for excellent reasons.

---

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [December 23, 2019, 2:18pm UTC](https://discourse.julialang.org/t/clearing-a-variable/32618/5 "2019-12-23T14:18:01Z")

</div>

Oh now that’s clever: so Julia thinks that T is still a variable, even though it is in fact a function?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [December 23, 2019, 2:49pm UTC](https://discourse.julialang.org/t/clearing-a-variable/32618/6 "2019-12-23T14:49:00Z")

</div>

Variable bind to things, which are all instances of types. E.g. `1` is such an instance, or as well the function `sin`. Variables (or more accurately “bindings”) which are `const` cannot be redefined. Also a variable which was non-const before cannot become `const` later. And as I said before, function definitions implicitly make `const` bindings, thus the problem you ran into.

However, just non-`const` variable can be re-assigned no problem:

```julia
julia> a = 5
5

julia> a = "Asdf"
"Asdf"

julia> const a = [1] # errors because of above explained stuff
ERROR: cannot declare a constant; it already has a value
Stacktrace:
 [1] top-level scope at REPL[3]:1

julia> a() = 1 # errors
ERROR: cannot define function a; it already has a value
Stacktrace:
 [1] top-level scope at none:0
 [2] top-level scope at REPL[4]:1

```

All in all, it’s sometimes a little bit annoying but not really all that much of a problem.

---

<div class="post-metadata">

**Author:** ![wsphillips](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wsphillips/32/6777_2.png) [@wsphillips](https://discourse.julialang.org/u/wsphillips)\
**Post date:** [December 23, 2019, 9:45pm UTC](https://discourse.julialang.org/t/clearing-a-variable/32618/7 "2019-12-23T21:45:05Z")

</div>

Note this is also almost never a problem if you follow naming conventions (like those suggested in the [style guide](https://docs.julialang.org/en/v1/manual/style-guide/)) Aside from readability, it helps make sure your bindings don’t bump into each other 😉

---

<div class="post-metadata">

**Author:** ![amca01](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amca01/32/5853_2.png) [@amca01](https://discourse.julialang.org/u/amca01)\
**Post date:** [December 24, 2019, 12:13pm UTC](https://discourse.julialang.org/t/clearing-a-variable/32618/8 "2019-12-24T12:13:46Z")

</div>

Yes, that’s very good advice indeed - thank you! The trouble is that I’m often chopping and changing between different language REPL’s (currently I am running Julia, Python, GNU Maxima, GNU Octave, all in different tmux panes) and I tend to use the same slapdash style in all of them. And of those four, I believe Julia is the only one without a variable clearing mechanism. However, I could certainly do with a slightly more disciplined approach, and maybe if I am more disciplined in my use of Julia I could be equally more disciplined in my use of other systems…
