CondaEnvironmentError: cannot remove current environment

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# CondaEnvironmentError: cannot remove current environment

The error "CondaEnvironmentError: cannot remove current environment" occurs for multiple reasons:

  1. Trying to delete a conda environment that is currently active.
  2. Using an incorrect command to delete a conda environment.
  3. Trying to delete the base conda environment which cannot be deleted.

condaenvironmenterror cannot remove current environment

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

Open your terminal and issue the following command to deactivate the environment before removing it.

shell
conda deactivate

conda deactivate and remove environment

After you deactivate the environment, issue the following command to remove it.

shell
conda env remove -n ENVIRONMENT_NAME # ๐Ÿ‘‡๏ธ Same as above conda env remove --name ENVIRONMENT_NAME
Make sure to replace the ENVIRONMENT_NAME placeholder with the name of the environment.

Use the conda env list command if you need to list the names of your conda environments.

shell
conda env list

list conda environments

You can use the conda info --envs command to verify the environment has been removed.

shell
conda info --envs

verify environment removed

If you get a permissions error on Windows, open CMD or Anaconda Prompt as an administrator and rerun the commands.

An alternative way to remove an environment is to use the -p flag.

# Removing a conda environment with the -p flag

The -n flag allows us to specify the name of the environment, whereas the -p flag allows us to specify the path to the environment.

Make sure the environment is not active by running the following command.

shell
conda deactivate

Use the conda info --envs command to get the path to your conda environment.

shell
conda info --envs

get path to conda environment

The path to the environment I want to remove is /home/borislav/anaconda3/envs/myenv, so I would issue the following command.

shell
conda env remove -p /home/borislav/anaconda3/envs/myenv # ๐Ÿ‘‡๏ธ Same as above conda env remove --prefix /home/borislav/anaconda3/envs/myenv

conda remove environment with p flag

If you created your conda environment with the -p or --prefix flag, you have to use the -p or --prefix flag to remove it.

You can use the conda info --envs command to verify the environment has been removed.

shell
conda info --envs

verify environment removed

You can also use the conda env list command to list your conda environments.

shell
conda env list

If none of the suggestions helped, you can delete the directory that contains your conda environment.

# Delete the directory that contains your conda environment

Make sure the environment is not active by running the following command.

shell
conda deactivate

Use the conda info --envs command to get the path to your conda environment.

shell
conda info --envs

get path to conda environment

The path in my case is /home/borislav/anaconda3/envs/myenv, so I would issue the following command.

Make sure to specify the correct path to your conda environment as rm -rf is a destructive command.
shell
# ๐Ÿ‘‡๏ธ For macOS and Linux rm -rf /home/borislav/anaconda3/envs/myenv

delete conda environment folder

If you are on Windows, you would issue the following command.

shell
rd /s /q "/home/borislav/anaconda3/envs/myenv"

Make sure to update the path based on the output of the conda info --envs command.

You can use the conda info --envs command to verify the environment has been removed.

shell
conda info --envs

verify environment removed

Note that you cannot delete the default base conda environment. The only way to delete the base environment is to uninstall Anaconda.

However, you can revert the base environment to its initial state.

# Reverting the base conda environment to its initial state

Make sure to deactivate your environment before reverting the base conda environment to its initial state.

shell
conda deactivate

Only run the following command if you want to revert the base conda environment to its initial state.

This would uninstall any of the additional modules you have installed into the environment.

shell
conda install --name base --revision 0

The command will remove the packages you installed in your base conda environment.

# Conclusion

To solve the error "CondaEnvironmentError: cannot remove current environment":

  1. Deactivate the conda environment before removing it.
  2. Use the conda env remove -n ENVIRONMENT_NAME command to remove the environment.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev