Delete a Python virtual environment when the interpreter tree is broken, the dependency set is no longer needed, or a project needs a fresh sandbox before packages are installed again. The project source stays outside the environment directory, so deleting the environment removes the local interpreter and installed packages without removing the application files.
A venv environment contains its own interpreter, activation helpers, private site-packages directory, and pyvenv.cfg file under a path such as .venv. Python virtual environments are disposable project directories, so the normal reset path is to save any needed package list, delete the environment directory, and create a replacement instead of trying to repair stale files in place.
On Linux or macOS, a POSIX shell can remove a project-local environment named .venv from the project root. Activation is only shell-local state, but terminals, editor tasks, or background jobs can still point at the old interpreter; close or deactivate them before deleting the directory. On Windows, the same deletion principle applies with different path and activation syntax.
$ cd ~/workspace/service-api
Running the delete commands from the environment's parent directory keeps the removal target short and easy to verify.
$ ls .venv/bin/python .venv/pyvenv.cfg .venv/bin/python .venv/pyvenv.cfg
A venv directory normally contains its own interpreter path plus a pyvenv.cfg file. Stop here if the path points anywhere unexpected.
$ .venv/bin/python -m pip freeze > requirements.txt
Calling the environment's interpreter directly captures packages from that environment even when the current shell is not activated against it.
$ cat requirements.txt certifi==2026.5.20 charset-normalizer==3.4.7 idna==3.18 requests==2.32.3 urllib3==2.7.0
$ printf '%s\n' "${VIRTUAL_ENV:-not active}"
/home/devops/workspace/service-api/.venv
If the variable prints the .venv path, deactivate the shell before deletion. If it prints not active, skip the next step.
(.venv) $ deactivate
Closing the shell also ends activation, but any editor, terminal tab, or background task still using the environment should be stopped separately.
$ rm -r .venv
The rm -r command recursively deletes the target directory, so verify the parent directory and environment name before pressing Enter.
$ [ ! -d .venv ] && echo ".venv removed" .venv removed
A direct directory check confirms that the old interpreter tree and installed packages are no longer present.