Conda remove all environments (except root)

Question:

I know I can delete a single environment with

 conda remove -n envname --all

but I often create multiple new environments for installing a specifiy package or testing it so I’ll regularly end up with 5-10 environments and it’s a pain to delete them after one another. Is there an easy way (for windows) to delete all environments except the root-environment?

Asked By: MSeifert

||

Answers:

As per my comment, you can get all the environments with one conda command and then try to loop through it and remove them individually. Here is one way you could do something like this. Note that you should replace anaconda_command_prompt_string with the appropriate string that your Anaconda Command Prompt calls. Also this code is probably quite fragile:

from subprocess import PIPE, Popen

anaconda_command_prompt_string = 'C:\Windows\system32\cmd.exe "/K" C:\Users\your_user_name\AppData\Local\Continuum\Anaconda3\Scripts\activate.bat C:\Users\your_user_name\AppData\Local\Continuum\Anaconda3'
p = Popen(anaconda_command_prompt_string, stdin=PIPE, stdout=PIPE, bufsize=1)
p.stdout.readline(), # read the first line

print >>p.stdin, 'conda env list' # write input
p.stdin.flush() 
p.stdout.readline()

p.stdout.readline()

p.stdout.readline()

p.stdout.readline()
envs = []


line = 'Anaconda'
while 'Anaconda' in line:
    line = p.stdout.readline()
    name = line.replace(' ', '').split('C:')[0]
    if 'root' not in name and 'n' not in name:
        envs.append(name)

for name in envs:
   command_string = 'conda remove -n {0} --all --yes'.format(name)
   print >>p.stdin, command_string
   p.stdin.flush()
   line = p.stdout.readline()
   while 'Complete' not in line:
      print line
      line = p.stdout.readline()
   print line
Answered By: Ringil

Removing all directories inside the envs subdirectory that resides inside conda does the job. This is generally in your user folder ~.

~.condaenvs

Not the most elegant answer. But I would just copy the names of all the environments from conda info --envs. Then make a bash (or .bat for windows) file with all the commands you need e.g…

conda remove -n env_name_1 --all -y
conda remove -n env_name_2 --all -y
conda remove -n env_name_3 --all -y
conda remove -n env_name_4 --all -y
conda remove -n env_name_5 --all -y

Or just copy and paste that into the terminal and it will sort you out!

If I was a little bash (or .bat) wizard (or could be bothered to do some googling) you could pipe the output from conda info --envs to generate the conda remove ... commands.

Answered By: henryJack

Mac/Linux based systems could remove all environments like this.

for i in `conda env list|awk '{print $1}'|egrep -v 'base|#'|tr 'n' ' '`;do echo $i;conda env remove --name $i;done
Answered By: teedak8s

This isn’t as satisfying as if conda had a built-in tool to do this (which it may), but you can delete the entire environment folder and recreate it in two lines.

rm -Rf /path/to/env_dir/envs/
mkdir /path/to/env_dir/envs/

Use conda info --envs or condo env list to find the path to your environments:

$ conda info --envs
# conda environments:
#
base                  *  /path/to/env_dir/
my_env                   /path/to/env_dir/envs/my_env
my_env_somewhere_else    /other/path/to/env_dir/envs/my_env_somewhere_else

Note, you may have environments in more than one location, so you may need to delete more than one directory, but they should show up in your list of environments.

Answered By: Kaleb Coberly
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.