Delete all packages installed in an environment

Suppose to try out a few things I create an environment as described here. I install some packages and test the things I want to. Now I want to delete all the packages that I installed in this environment. Is there a command to do that? I believe that if a package that I installed in this trial environment was also installed in some other environment then it is not reinstalled on my computer. So, when I want to delete all the packages installed in this environment, I just want to delete those that were exclusively added for this environment. Is there a way to do that, without naming each individual package from Project.toml?

Thank you

There’s a ]gc command in pkg mode that will delete “unreachable” packages.

2 Likes

Thank you @savq. Should this be called from the global environment after I have deleted the “temp” environment?

Note that this is done automatically every so often by the package manager. If you want to get rid of the packages immediately, then you probably want to call Pkg.gc as follows:

julia> using Pkg, Dates

julia> Pkg.gc(; collect_delay=Day(0))
      Active manifest files: 110 found
      Active artifact files: 455 found
      Active scratchspaces: 57 found
     Deleted no artifacts, repos, packages or scratchspaces

And yes, you can call it from the global environment after deleting the “temp” environment.

2 Likes

To be clear, there’s no direct way to uninstall a package by name precisely because another environment might still be using it, as you said. Deleting unwanted environments and rm-ing the package from kept environments is how you communicate to Pkg.gc that the package is safe to uninstall. If you’re just trying stuff out in a temporary environment, you can make one that Pkg deletes for you once you exit.

2 Likes

Thank you all. I appreciate your help.