How to clear variables and/or whole work space

As I have answered on SO the currently recommended way is to do all your work inside a custom defined module (i.e. do not define variables in Main module but in some submodule). Then you can reload such a custom module. Unfortunately it is not super convenient, but works.

Here is the link Workflow Tips · The Julia Language I to the Julia Manual that describes this workflow.

2 Likes

thank you

Based on Julia’s documentatio: “Julia does not have an analog of MATLAB’s clear function”, which clears all variables in the workspace. However, in VsCode, it has a convenient bind to Alt+J Alt+R that restart the REPL, cleaning out all variables in the workspace.

2 Likes

Hi Guys, Ctrl-L clears the latest part of the console. but none of the above (clearconsole(), CTRL-L, workspace()) works to give me a clean console to work with. I am using VSCode to code in Julia. anyone know what cleans up the console please?

Clearing the Console

Ctrl+L should clear all the text from the terminal. You might need to set the keyboard shortcut in VSCode by searching in the command palette then clicking on the settings cog.
image
image

This is a VSCode command though, not a Julia command. Some of convenience functions that were implemented in the Atom Julia extension like clearconsole and workspace have not been implemented in the VSCode extension yet. I opened an issue about adding them a while back.

Clearing the Workspace

Clearing all variables is not currently possible in Julia. See the documentation about it here. You can set variables equal to nothing, but the only way to get a truly clean REPL is to restart it.

  • In VSCode, you can restart with the Restart REPL command (Alt+J, Alt+R by default).
    image

  • In a standalone terminal window, you can type exit() in the REPL then julia in the terminal shell.

1 Like

Thank you it worked

That’s right. In any case, even just setting variables to nothing might be useful in order to release memory, if there are variables bound to large objects that won’t be used anymore, and you want to keep the Julia session alive (loaded packages, compiled functions, etc.).

This simple function may be used to unbind the objects of all non-constant globals in Main (functions, modules, const globals, etc. would remain safely untouched):

function unbindvariables()
    for name in names(Main)
        if !isconst(Main, name)
            Main.eval(:($name = nothing))
        end
    end
end
1 Like