[Conda] Error: CondaEnvRemoveError — failed to remove environment — How to Fix It
Summary
The CondaEnvRemoveError: failed to remove environment appears when Conda cannot delete an environment folder because some files are still open, locked, or protected. This happens when processes like Jupyter, VS Code, or Spyder are using the environment, or when file permissions prevent deletion. Closing active sessions, deactivating the environment, and manually deleting its folder typically resolve the issue.
Context
When you remove an environment with conda remove -n <env> --all, Conda must delete all files and metadata associated with that environment. If any process holds a file handle inside that directory (e.g., a Jupyter kernel, Python shell, or IDE), Conda cannot complete the operation. In some cases, antivirus software or Windows file indexing also locks files, causing permission errors or incomplete removal attempts.
Probable Cause
- The environment is still active or being used by another process.
- File locks or residual processes from Jupyter, VS Code, or Spyder.
- Insufficient permissions to modify or delete environment files.
- Corrupted metadata in
conda-metapreventing normal cleanup. - Antivirus or file indexing software keeping files open in the background.
Quick Fix
Follow these steps to safely remove the environment:
- Deactivate any active Conda environment:
conda deactivate
- Close all IDEs and terminals using the environment:
- Shut down Jupyter notebooks.
- Close Spyder, VS Code, and Python consoles.
- Retry removal:
conda remove -n <env-name> --all
- If the error persists, manually delete the environment folder:
# Windows
rmdir /S /Q C:\Users\\Anaconda3\envs\
# Linux/macOS
rm -rf ~/anaconda3/envs/
- If you encounter permission issues, run Conda as Administrator or with sudo:
# Windows
Open "Anaconda Prompt" → Right-click → Run as Administrator
# Linux/macOS
sudo conda remove -n --all
- If lock files remain, remove them manually:
rm ~/.conda/locks/*
- After cleanup, recreate the environment if needed:
conda create -n python=3.11
Full Example
Example scenario:
conda remove -n myenv --all
returns:
CondaEnvRemoveError: failed to remove environment
PermissionError: [Errno 13] Permission denied: 'conda-meta/history'
Solution steps:
conda deactivate
conda remove -n myenv --all
If still blocked, remove manually:
rm -rf ~/anaconda3/envs/myenv
The environment is now successfully removed and ready for recreation.
Pitfalls & Debug
- Symptom: Permission denied — Fix: Run as Administrator or use
sudo. - Symptom: “Resource busy” — Fix: Close open IDEs and terminals referencing the environment.
- Symptom: Partial deletion — Fix: Delete remaining folder manually.
- Symptom: “Lock file exists” — Fix: Remove leftover lock files from
~/.conda/locks. - Symptom: Antivirus interference — Fix: Temporarily disable real-time protection during removal.
Validation & Next Steps
Confirm that the environment was deleted successfully:
conda env list
If it no longer appears, the deletion succeeded. You can then recreate it safely:
conda create -n myenv python=3.11
conda activate myenv
Always deactivate environments before removal to prevent this issue from recurring.
Sources
Anaconda Documentation — Managing environments
Stack Overflow — community troubleshooting for Conda environment removal
conda-forge — common lock and permission handling practices
Linux man pages — rm, sudo, and file permission handling