59

I have a conda virtual environment with several unused packages installed inside it (either using pip install or conda install).

What is the easiest way to clean it up so that only packages that are actually used by my code remain, and the others are uninstalled?

Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126
  • 8
    You might want to take a look at `conda clean --packages`. In particular, you could try it with the "dry-run" setting first -- i.e. `conda clean --packages --dry-run`. I think it is only looking for packages which are not used in any conda environments. So it won't check to see if you are using it in the code itself but it could be useful. – Paul Mar 30 '16 at 15:58

3 Answers3

64
conda clean --yes --all

will sanitize everything. But take note: if you ever want to do any type of --offline operations, don't use --all; be more selective.

kalefranz
  • 4,612
  • 2
  • 27
  • 42
  • 36
    This doesn't uninstall packages, it only deletes the downloaded package files. – Spirko Feb 17 '18 at 14:42
  • 3
    do you know what the difference is to `conda clean --yes --packages`? Interestingly, the --packages finds twice as much data than the --all option for me. – Agile Bean Dec 04 '18 at 09:29
  • 2
    @Agile Bean The difference between conda clean --yes --all and conda clean --yes --packages is that the packages are only the extracted folders. All of the other files (.tar.bz2, .conda, that is: tarballs) are not cleaned by --packages. If you want to clean only the tarballs, you would need conda clean --yes --tarballs References: https://stackoverflow.com/questions/44708392/anaconda-python-delete-tar-gz-in-pkgs – questionto42 May 20 '20 at 19:55
  • 3
    @Lorenz thanks for the clarification! then i just wonder why the `--packages` found more files than the `--all`... – Agile Bean May 21 '20 at 04:55
  • @AgileBean I added a separate answer. – questionto42 May 21 '20 at 16:26
  • I once ran `conda clean --all` and am now facing problems/breakage with `--offline` installs for *some* packages, despite a fresh install. Can you please explain a) why `--all` is problematic for `--offline` and b) how to fix the situation? – mara004 Aug 31 '23 at 12:41
26

@AgileBean I try an answer to your comment's question on why --packages gives you more results than --all. This is still related to the main question how to uninstall, hopefully.

The difference between

    conda clean --yes --all

and

    conda clean --yes --packages

is that the packages are only the extracted folders. All of the other files (.tar.bz2, .conda, that is: tarballs) are not cleaned by --packages.

If you want to clean only the tarballs, you would need

    conda clean --yes --tarballs

References: Anaconda Python: Delete .tar.gz in pkgs

Here is an example of the differences. Mind that --all includes --packages in a real run, but it does not show --packages results in dry-run (very strange!, see the following screenshot, it just stops at DryRunExit: Dry run. Exiting.)

conda clean --all --dry-run vs. conda clean --packages --dry-run

Which differences exist that could explain that you find more with --packages than with --all?

  1. As said before, my first guess is that you only used dry-run option which will not show you the cleaned --packages when you run conda clean --all --dry-run. Therefore see this real run from conda clean --all:

    conda clean --all

  2. The 2 warnings could be interesting:

    WARNING: C:\Users\Admin\.conda\pkgs does not exist                                                                                                                                           
    WARNING: C:\Users\Admin\AppData\Local\conda\conda\pkgs does not exist
    

    But if you do not dry-run, but really run --all, you get the same, because --all includes the --packages and thus its warnings as well. This, again, cannot be seen when you use dry-run.

  3. A good reason could be that you have once cleaned your packages with --tarballs or that you have simply removed some tarballs manually so that your unzipped packages outnumber your tarballs in the --dry-run.

  4. You might have unzipped a lot of packages manually into the cache folder, e.g. the manual installations from git and all of the other installations that do not offer conda / pip install and then again, in --dry-run, the --all exits without showing the --packages.

  5. Perhaps you find another thing in the docs? https://docs.conda.io/projects/conda/en/latest/commands/clean.html. It says about symbolic links: "WARNING: This does not check for packages installed using symlinks back to the package cache." As --packages is part of --all, this is still no explanation of your difference.

I guess that the reason for your --packages > --all issue is that conda clean --all --dry-run does not show the results of the --packages, although it cleans them as well, so that you do not actually have that issue ;).

questionto42
  • 7,175
  • 4
  • 57
  • 90
6

For what it's worth, I noticed the following...

  • conda clean --all --dry-run gave me a rough total of 2GB
  • conda clean --packages --dry-run gave me a rough total of 6GB

So same discrepancy as observed by OP...

When I next did conda clean --tarballs --dry-run I noticed it also gave me 2GB, strange... Comparing output of first and last commands it seems conda clean --all --dry-run only showed me the tarballs, no mention of the packages

I went ahead, did conda clean --tarballs and then reran the conda clean --all --dry-run... guess what? It now showed the packages (after mentioning there were no tarballs, which is logical as I just cleaned them)

My conclusion... when there are still tarballs in the cache, conda clean --all --dry-run does not provide you the full picture of what will/could be removed

kochuyt
  • 69
  • 1
  • 3
  • I run the same in my conda environment (using Anaconda prompt) on windows. "conda clean --all --dry-run" is listing unused packages as well. (mycondaenv) C:\myrepo>conda clean --packages --dry-run Will remove 685 (9.45 GB) package(s). DryRunExit: Dry run. Exiting. (mycondaenv) C:\myrepo>conda clean --all --dry-run Will remove 922 (2.65 GB) tarball(s). Will remove 1 index cache(s). Will remove 685 (9.45 GB) package(s). There are no tempfile(s) to remove. There are no logfile(s) to remove. DryRunExit: Dry run. Exiting. – learningstack Jun 23 '22 at 10:18
  • @learningstack hmmm... strange... triggered by your comment I tested it again and as far as I can see I still get the same behaviour: if there are tarballs conda clean --all --dry-run does not talk about packages that can be cleaned... Dazed and confused... What conda version are you on? (v4.12 here) – kochuyt Jun 30 '22 at 17:46