MSeifert
MSeifert

Reputation: 152677

Conda remove all environments (except root)

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?

Upvotes: 37

Views: 51543

Answers (5)

teedak8s
teedak8s

Reputation: 780

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

for environment_name in $(conda env list | awk '{print $1}' | grep -v -e '^base' -e '^#' | tr '\n' ' ')
do
    echo "$environment_name"
    conda remove --name "$environment_name"
done

Upvotes: 24

Kaleb Coberly
Kaleb Coberly

Reputation: 460

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.

Upvotes: 0

Gustavo Muenz
Gustavo Muenz

Reputation: 9552

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

~\.conda\envs\

Upvotes: 29

henryJack
henryJack

Reputation: 4614

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.

Upvotes: 6

Ringil
Ringil

Reputation: 6537

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

Upvotes: 3

Related Questions