102

TL:DR: How can I remove all installed packages from base?

I installed a bunch of machine learning packages in my base conda environment.

I've now created a ml environment for machine learning, and wish to reset my base environment by removing all the packages installed there.

I've tried:

% activate base
% conda uninstall -n base --all

CondaEnvironmentError: cannot remove current environment. deactivate and run conda remove again

Apparently, I can't remove packages from the current environment(?!), so lets switch to my ml environment first:

% source activate ml
% conda uninstall -n base --all

CondaEnvironmentError: cannot remove root environment,
       add -n NAME or -p PREFIX option

Orright, I'll use -p then...

% conda uninstall -p ~/.local/share/miniconda3 --all

CondaEnvironmentError: cannot remove root environment,
       add -n NAME or -p PREFIX option

How do I uninstall all installed packages in the base or root environment?

Community
  • 1
  • 1
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • 1
    "conda uninstall -n base --all" should do the job. Have you tried running that command when the environment is deactivated? %deactivate base %conda uninstall -n base --all – Tina Iris Oct 16 '18 at 08:40
  • @TinaIris Nope, that didn't help - same error message. – Tom Hale Oct 16 '18 at 11:39
  • 17
    Had the same problem. My lesson: I'll never use the base environment again. – NoDataDumpNoContribution Nov 11 '18 at 21:40
  • The nuclear option is to delete `~/anaconda` or `~/miniconda` and reinstall conda. – Jasha Sep 29 '21 at 21:01
  • 9
    Less nuclear option - **rename** your `~/miniconda` folder, reinstall `conda`, then copy environments you want to keep from the old renamed folder into the newly reinstalled one. This way you have fresh new `base` env, but you don't need to reinstall your other envs. *Then* you can delete the renamed backup folder. – jena Nov 10 '21 at 14:02
  • 3
    @jena : For my case, your solution is the best and the fastest, and, IMHO, should be an answer. `mv ~/anaconda3 ~/anaconda3-old` and after reinstall `cp -r ~/anaconda3-old/envs/. ~/anaconda3/envs/ ` (For me it was anaconda3 but it works.) – phili_b Jul 21 '22 at 16:54

8 Answers8

68

Apparently, I can't remove packages from the current environment(?!)

It's not that, but instead that you can't remove the base environment, which is what the --all flag in this context would do. You can't uninstall all packages in base because that's where the conda executable lives. Instead, what you likely want to do is uninstall all user-installed packages.

Full Reversion (Not Recommended)

One way to do this is to revert your environment back to the original state before you installed any additional packages:

# Not generally recommended!
conda install --revision 0

Be aware the multiple users have reported this breaking their Conda installation. I definitely would not run this on an installation that you have had for a long time or has many revisions. This is not about revision "0" being inherently bad, but rather that reverting too far back in time can be bad. That is, revision "1", "2", etc., could also be very old and lead to exactly the same problems.

Most importantly: Always review the proposed transactions in the base env! This is where Conda lives and unfortunately the safeguards against breaking an installation are not comprehensive.

If you really want a clean start, then export your envs to YAMLs and reinstall a fresh Miniconda.

Partial Reversion

You can also look for other previous states that might be less of a regression, but still get rid of whatever packages you think you've unnecessarily accumulated.

conda list -n base -r

In the end, you'll probably want to upgrade conda right after, since it will also revert any updates to the base packages.


Errors, oh my!

While the above is the correct way to revert, I encounter the error:

CondaRevisionError: Cannot revert to 0, since ::contextlib2-0.5.3-py35_0 is not in repodata.

As an aside, this sort of worries me because it seems to indicate that the state of my Conda environment from two years ago is no longer reproducible from the state of the upstream channels.

Under this situtation, I don't know a clean way to solve this other than comparing all the revision 0 packages to your current install and then uninstalling the difference. But again, a clean install of Miniconda seems like a nicer solution.

Generally, I've found that treating envs as immutable and installing as little as possible in base is the safest and most reliable way to use Conda.

merv
  • 67,214
  • 13
  • 180
  • 245
  • I also got a bunch of errors when reverting to revision 0. I ended up: backing up reinstalling, then copying over my `etc` and `envs` directories from the backup. – Tom Hale Oct 17 '18 at 12:46
  • 9
    Careful with `conda install --revision 0` I tried this and it reverted ALL my environments, not just the base! Not a huge problem for me, but could be catastrophic if you didn't have backups. – Takver Feb 15 '19 at 12:46
  • @Takver, were you in an activated env (e.g., **base**) when you ran the command? Since you're already set up to recover, maybe you could test `conda install -n base --revision 0` and report whether that also results in this behavior. I should add that given the report generated when `--dry-run` flag is used, there is no indication that you should experience what you say, so you should report to this to [the conda repository](https://github.com/conda/conda/issues). – merv Feb 15 '19 at 14:35
  • 1
    I was in base yes. But although it was fine to recover, it took some time so I don't want to do it again! Should I still report it? – Takver Feb 15 '19 at 15:58
  • I tried this (`conda install --revision 0`) and it seemed to work but now it says `This environment has previously been operated on by a conda version that's newer than the conda currently being used. A newer version of conda is required.` and I can't update conda anymore or do anything. – Bill Jun 30 '19 at 03:03
  • @Bill Good to know! It'd probably be best to ask a new question, to get more visibility to your issue. One thing to try is using the `--force` flag to see if that will get the Conda updated. I don't know exactly how Conda logs/detects versions. – merv Jun 30 '19 at 03:10
  • 2
    Thanks @merv. Actually, there is a process [here](https://stackoverflow.com/questions/54542706/how-can-i-get-unstuck-from-condaupgradeerror-a-newer-version-of-conda-is-requir) to get unstuck from a downgraded conda situation but I left the comment here to point out this trap. There should be a better way to remove all packages than this `install --revision` method. – Bill Jun 30 '19 at 03:15
  • You do not want to do `conda install --revision 0` on (base) because this removes everything. (base) is not merely a "base env", it is analogous to the root. This is easy to prove this because if you do `conda list` while in (base) the **anaconda-client** and **anaconda-navigator** will be in that list. If you do `conda list` while in (MyValuableEnv) those two will not be there. You can do `conda install --revision 0` on your own environment but not in (base). This really should be flagged and disabled by Anaconda. – Edison Sep 04 '19 at 00:07
  • That command broke my conda. I cannot use "conda activate" command now. – Halil İbrahim Oymacı Jan 08 '20 at 12:00
  • @HalilİbrahimOymacı okay, thanks for reporting that. Since multiple users have had this issue, I've added an explicit warning against using `--revision 0`. – merv Jan 08 '20 at 17:33
  • `conda install --rev 1` won't even work for me due to `PackagesNotFoundError: The following packages are missing from the target environment:` – ijoseph May 23 '20 at 20:01
  • @ijoseph this can happen for many reasons. If you want to try to resolve it, you can ask as a new question. – merv May 23 '20 at 20:13
  • 3
    @merv thanks for the reply. Ended up just killing my entire `anaconda` installation and reinstalling (`miniconda`). It's so much more usable now -- used to take on the order of 5 minutes to the `solving environment` step; now it's on the order of 5 ms. – ijoseph May 23 '20 at 20:18
  • I had the same issue, I ended up exporting to yml files all my env and reinstalling miniconda as well then restoring all my env. – sechstein Jul 27 '20 at 13:14
  • 2
    You propose a way (revision) that you do not recommend and then don't give a recommended way. – gargoylebident May 18 '21 at 08:56
  • @stackexchange_account1111 Sorry if that was unclear. I recommended the *partial reversion* approach. That is, rather than fully revert to the original state of the environment I recommend carefully identifying a previous revision that removes as few packages as possible, but as many as necessary. The strong caution against full reversion may be overstated, but multiple users reported it breaking their Conda installation. The key thing to keep is mind is to thoroughly review any proposed changes before accepting them. – merv May 18 '21 at 10:37
  • 1
    At that point might as well remove packages manually. Or reinstall Anaconda. Too tedious / too much of a time waste. – gargoylebident May 18 '21 at 12:53
  • My rev 0 has all user-installed packages plus conda. Basically, the installation is now unusable. – malthe May 27 '21 at 08:37
  • @malthe are you sure the user-installed packages were actually installed via Conda? Conda isn’t going to remove stuff installed with pip. – merv May 27 '21 at 13:44
19

I had the same problem as you did, this is what I did:

  1. backup my conda-env:

    i. activate the env i want keep, such as 'ml'

    ii. type conda-env export > /path/to/save/file/environment.yml

  2. activate base and revert base env to initial: type conda install --revision 0, this should take some time...(this command did not revert ALL my envs, just the BASE env)

  3. load your .yml file: type conda env create -f /path/to/save/file/environment.yml

Djensen
  • 1,337
  • 1
  • 22
  • 32
Aaayue
  • 191
  • 1
  • 4
  • 1
    This may fail if the packages in your environments are no longer present in the repos/channels. I instead renamed my miniconda folder (to e.g. miniconda_old), then you would do your thing (I reinstalled miniconda, as I managed to completelly mess it up), then copy envs from the miniconda_old into newly installed miniconda. The envs then worked fine. – jena Nov 10 '21 at 15:43
3

You could try the following for remove the all installed packages from your anaconda environment,

$ conda list | awk {'print $1'} >> packages

$ for i in `cat packages`; do echo $i; done

$ for i in `cat packages`; do conda remove --force $i -y; done

3

simple solution:

select your environment via:

conda activate <env> e.g. conda activate base

use:

pip freeze > requirements.txt

and than:

pip uninstall -r requirements.txt -y
canunal
  • 47
  • 2
  • 5
    This is a good idea, but unfortunately it deletes the conda python package so I can't activate my conda environment. I would recommend instead `pip freeze | grep -v conda > requirements.txt`. – Wes Aug 02 '22 at 20:16
  • 3
    Yup, it deleted my conda too. @Wes 's version is solid tho – l -_- l Oct 29 '22 at 23:27
  • 1
    It only deletes packages managed by pip. There are some, managed by conda. – Kiryl A. Feb 09 '23 at 14:43
  • 2
    @Wes, to make the operation safer, we can create a new conda environment and check what packages conda automatically installs. Avoid these packages when uninstalling manually. – Mr.Spock Apr 04 '23 at 06:35
  • I tried @Wes's version and sadly it messed up my conda installation (e.g. could not run `conda update conda` Darn... had high hopes – Bryan P Jun 08 '23 at 02:01
  • @Wes's version breaks my conda, too. – truth Jul 03 '23 at 20:51
3

If you just want to remove the unused packages from the deleted environments that are still cached in base, issue

conda clean -a -y 

That cleaned up 74 GB from my base :)

Nicole Finnie
  • 1,370
  • 13
  • 12
1

I used the following command to remove all installed packages (not environment):

$ conda remove `conda list|awk {'print $1'}|tr '\n' ' '`
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • this is brilliant! in my case, it was `conda list|awk 'NR>3 {print $1}'|tr '\n' ' '` cus first three rows of the conda list output were some headers – Marek Piotrowski Jun 11 '23 at 19:20
0

I've been searching for the best solution to my problem similar to yours for a while on how to remove all packages I installed in the base env. But it turns out the best solution for me is to delete all the packages and envs folder in my disk and uninstall and install the Anaconda again.

Tri Dawn
  • 540
  • 6
  • 11
0

Though a bit brute force, and time consuming if there are lots of environments, it might just be easiest to:

  • Reinstall conda

Before proposing this I tried a few approaches from the other answers and ended up with an unusable conda install. Reinstalling was what worked in the end.

Note: be sure to save/export any hard to recreate environments as described in this question

Likely would also work with miniconda, but I have not tried.

Bryan P
  • 5,900
  • 5
  • 34
  • 49