# How to delete a function?

**URL:** <https://discourse.julialang.org/t/how-to-delete-a-function/108422>\
**Category:** New to Julia\
**Tags:** question, functions\
**Created:** [January 6, 2024, 3:26am UTC](https://discourse.julialang.org/t/how-to-delete-a-function/108422 "2024-01-06T03:26:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 6, 2024, 3:26am UTC](https://discourse.julialang.org/t/how-to-delete-a-function/108422/1 "2024-01-06T03:26:04Z")

</div>

I want to experiment with a small function. Before defining it, I want to make sure that the function name is not used before, or it does not exist in the workspace.

How can I do it? In Matlab, it is simple with ‘clear’

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [January 6, 2024, 3:32am UTC](https://discourse.julialang.org/t/how-to-delete-a-function/108422/2 "2024-01-06T03:32:59Z")

</div>

You can’t, unfortunately. There are some workarounds people use but there’s no obvious solution.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [January 6, 2024, 4:00am UTC](https://discourse.julialang.org/t/how-to-delete-a-function/108422/3 "2024-01-06T04:00:43Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> Before defining it, I want to make sure that the function name is not used before, or it does not exist in the workspace.

If it errors, it does not exist

```julia
julia> blabla
ERROR: UndefVarError: `blabla` not defined

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 6, 2024, 4:05am UTC](https://discourse.julialang.org/t/how-to-delete-a-function/108422/4 "2024-01-06T04:05:13Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> Before defining it, I want to make sure that the function name is not used before, or it does not exist in the workspace.

There’s no workspaces in Julia. If you mean module, including the default `Main` of the REPL, you can use `hasproperty` to check if it has a global variable and `isdefined` to check if it is also assigned, if you don’t want to check with an error. You can’t remove a variable from a module, even if you can reassign it to possibly free the previous instance for the garbage collector; this is important for optimizations and keeping code working.

In interactive practice, it’s fine to make a few throwaway `const` variables, and most of the time I’m just redefining methods of the same function. If you really find yourself in the position of making many distinct functions, you could avoid making as many variables by assigning anonymous functions to one non-`const` variable. You can’t add methods with a non-`const` name alone e.g. `foo(x)=1`, but you can add one with `(::typeof(foo))(x) = 1`. Just be aware that a non-`const` global variable will introduce runtime dispatch in callers, so I’d stick with top-level calls for experimentation with them.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [January 6, 2024, 6:09am UTC](https://discourse.julialang.org/t/how-to-delete-a-function/108422/5 "2024-01-06T06:09:16Z")

</div>

As said by others:

- If what you want is to make sure that the function name is not used before, or it does not exist, you can query with the function `isdefined`, or easier with the macro `@isdefined` (e.g. `@isdefined foo` to query if there is anything called `foo` in the current module`.
- If you want to delete a function (or whatever other type of object’s) name, unfortunately [you can’t](https://docs.julialang.org/en/v1/manual/faq/#How-do-I-delete-an-object-in-memory?). (There are other useful FAQs in the same page.)

The nearest thing to Matlab’s workspaces in Julia are modules. It is possible to [switch between modules in the REPL](https://docs.julialang.org/en/v1/stdlib/REPL/#Changing-the-contextual-module-which-is-active-at-the-REPL), which is the closest that you’ll get to `clear all` in Matlab (without actually deleting anything), but it’s better to adapt your workflows so that you don’t feel the need of “deleting variables”, which is unnatural in Julia.

Since you are asking several questions related to transitioning from Matlab to Julia, let me recommend you to spend some time reading some of the many resources that the community has being producing for such situations. There is a thread from a few year ago where they started to comment on this:

> [@TL;DR tips for Julia users coming from Matlab](https://discourse.julialang.org/t/tl-dr-tips-for-julia-users-coming-from-matlab/41093):
>
> While the Julia Discourse and Slack communities are incredibly helpful, the responses can lean more technical. I thought it’d be nice to collect TL;DR advice for Julia summarized in simple terms. For example, in [this thread on “why use tuples”](https://discourse.julialang.org/t/when-to-use-tuples/18675), in addition to the responses about tuples helping with type inference, it’d be nice to also have a simple “they are faster than arrays (e.g., to splat)”. Non-technical terminology is also helpful for folks like myself, who are transitioning from Matlab o…

And some other resources that might be useful too (you’ll find many more with any search engine):

[https://en.wikibooks.org/wiki/Julia\_for\_MATLAB\_Users](https://en.wikibooks.org/wiki/Julia_for_MATLAB_Users)

> **[GitHub - brenhinkeller/JuliaAdviceForMatlabProgrammers: Learning to love...](https://github.com/brenhinkeller/JuliaAdviceForMatlabProgrammers)**
>
> Learning to love dispatch-oriented programming. Contribute to brenhinkeller/JuliaAdviceForMatlabProgrammers development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 6, 2024, 9:20am UTC](https://discourse.julialang.org/t/how-to-delete-a-function/108422/6 "2024-01-06T09:20:27Z")

</div>

> [@heliosdrm](#):
>
> The nearest thing to Matlab’s workspaces in Julia are modules. It is possible to [switch between modules in the REPL](https://docs.julialang.org/en/v1/stdlib/REPL/#Changing-the-contextual-module-which-is-active-at-the-REPL), which is the closest that you’ll get to `clear all` in Matlab

I think MATLAB’s workspaces are [so particular](https://www.mathworks.com/matlabcentral/answers/99602-how-can-i-use-global-variables-and-matlab-workspaces#answer_108950), it’s better to just say they’re different from modules. This discrepancy isn’t by accident, MATLAB’s workspaces in part make some interactive practices like `clear`ing much more feasible, though it’s worth reminding that [MATLAB documentation](https://www.mathworks.com/help/matlab/matlab_prog/techniques-for-improving-performance.html) discourages large `clear`s. As I’ve [written before](https://discourse.julialang.org/t/non-deterministic-memory-allocation/104941/6), we can’t delete global variables without really bad consequences, for the same reason that MATLAB protects global variables into a separate global workspace away from the script’s base workspace.

There’s merit to seeking parallels between MATLAB and Julia functionality, but I think there is also merit to learning a language on its own terms enough to readily identify the reasons a feature is absent. This is just tougher when the languages have more fundamental differences, and MATLAB does many things differently from even other interactive high-level languages, with the exception of its clone Octave.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [January 8, 2024, 1:21pm UTC](https://discourse.julialang.org/t/how-to-delete-a-function/108422/7 "2024-01-08T13:21:44Z")

</div>

Simplest solution is to restart the REPL.

**VSCode:** Alt+J, Alt+R  
**Terminal:** `exit()`, `julia`

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [January 8, 2024, 1:47pm UTC](https://discourse.julialang.org/t/how-to-delete-a-function/108422/8 "2024-01-08T13:47:15Z")

</div>

> [@heliosdrm](#):
>
> Since you are asking several questions related to transitioning from Matlab to Julia, let me recommend you to spend some time reading some of the many resources that the community has being producing for such situations.

see also [Noteworthy Differences from other Languages · The Julia Language](https://docs.julialang.org/en/v1/manual/noteworthy-differences/)
