Author: Marco Jeffrey Pansa

Date: 06-05-2024

Tags: conda

Clean your conda environments the easy way

In many parts of the world its spring time now and that means time to do some cleaning. If you are anything like me you probably have gathered a ton of conda environments over the past months / years. Most people never care to remove any, but chances are they are taking away a good chunk of your disk space. So lets use some terminal magic to remove them without breaking a sweat. Of course we can use conda env list and then try to remember the correct names and then do conda env remove -n *name* one by one. But its annoying and I get why people never get to clean the envs properly.

For the purpose of this post I created some dummy environments. If you dont have any envs to clean and want to make some dummies, here you go:

echo "1 2 3" | xargs -n 1 -I {} conda create -n "test_env_{}"

image

Disclaimer:

  • this will work on linux and macos, for windows maybe try porting the script using your friendly neighbourhood LLM
  • you might not have fzf installed, brew install fzf on macos -> apt install fzf on ubuntu. For other distros go check the fzf github :)

Now lets actually do some cleaning:

the general plan is to use conda env list , pipe that into fzf using multi select, do a minimal text transform and then pipe that into conda env remove -n *name*. fzf stands for fuzzy finder and is really just an awesome multipurpose tool and once you start using it you see opportunities to use it again for so many different things. For convenience I would advise to add this to your bashrc/zshrc because you will probably want to use it from time to time. Ofc you can also just copy the function body for one time use ;)


function conda_remove() {
  conda env list | \
  fzf --header "Multiselect envs to remove (use tab to select)" -m | \
  awk '{print $1}' | \
  xargs -I {} conda env remove -n {} -y
}

a quick recap:

  • we use conda env list to list all envs
  • we use fzf to have an interactive fuzzy finder interface to select envs
  • awk is a powerful text-processor, by default it splits every line on space characters into columns. Here we basically just tell it to select the first column which represents the name of the env
  • xargs processes our output line by line and calls it with whatever function we specify.

when you run this you should see the fzf interface popping up. You can now either navigate with control+j/k or arrow keys and use the search to fuzzy find across all your conda envs. Select or deselect an env using the tab key. Hit enter to remove all selected envs or cancel with Esc.

image

et voilĂ , all gone in seconds!

image

One more really useful thing:

Of course we can also make use of fzf to activate conda environments without the pain of always looking up the correct names in conda env list first and making millions of typos. This one is really only useful if you put this into your zshrc/bashrc. Its especially handy when you tend to use long env names. Now I only have to type sc and do a fuzzy find over all envs and hit the one I need. No more typos :)


function sc(){
  conda activate $(conda env list | fzf --header "Select conda env:" | awk '{print $2}')
}

Ok, now really the last thing:

This also works super great for investigating / removing pip packages.


function unpip() {
  pip list | \
  fzf --header "Multiselect packages to remove (use tab to select)" -m | \
  awk '{print $1}' | \
  xargs -I {} pip uninstall {} -y
}

Now you can also use this for moving certain files to other directories with ease by using fzf with multiselect, delete files / folders or whatever. If you have other cool use cases let me know on twitter :)