# How to Force a Clean PyTorch Reinstall by Deleting `.venv` and `uv.lock`

> Force a clean PyTorch reinstall by deleting .venv and uv.lock. Recreate your virtual environment and resolve dependencies from scratch for a fresh PyTorch installation.

- Repository: [Th3Unknovvn/uv-install-torch](https://github.com/baonguyen6742/uv-install-torch)
- Tags: how-to-guide
- Published: 2026-02-26

---

**Deleting both `.venv` and `uv.lock` forces uv to recreate the virtual environment and re-resolve dependencies from scratch, ensuring a completely clean PyTorch installation without stale CUDA binaries or corrupted wheels.**

The `baonguyen6742/uv-install-torch` repository demonstrates the canonical workflow for managing PyTorch with uv. When CUDA wheels fail to load or dependency resolution drifts, wiping these two artifacts is the fastest path to a pristine environment.

## Why Delete `.venv` and `uv.lock`?

The uv package manager relies on two persistent artifacts that can harbor stale state:

* **`.venv`** – The virtual environment directory containing compiled wheels, including the heavy CUDA binaries for `torch`, `torchvision`, and `torchaudio`. If a previous install pulled the wrong platform wheel or a corrupted file, uv will continue re-using those bad artifacts until the directory is purged.
* **`uv.lock`** – The lockfile that pins exact versions and platform-specific resolutions derived during the first sync. Deleting it forces `uv sync` to recompute the dependency graph from [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml) from scratch, picking up updated indexes or changed extras.

According to the source at [`README.md`](https://github.com/baonguyen6742/uv-install-torch/blob/main/README.md) lines 73‑74, this cleanup is explicitly recommended when installation problems persist or when switching CUDA versions.

## Step-by-Step Clean Reinstall Procedure

### 1. Remove the Virtual Environment and Lock File

Execute a recursive delete to wipe the existing environment and resolution state:

```bash
rm -rf .venv uv.lock

```

This command eliminates the compiled Python environment and the frozen dependency graph, guaranteeing that the next sync starts fresh.

### 2. (Optional) Prune the uv Global Cache

If a corrupted wheel persists in uv’s global cache, clear it before reinstalling:

```bash

# Remove all unused caches

uv cache prune

# Or target only PyTorch-related artifacts

uv cache clean torch

```

Pruning prevents the resolver from re-injecting a bad wheel into the new `.venv`.

### 3. Reinstall Dependencies with CUDA Support

Run `uv sync` with the `cu124` extra defined in [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml). This extra maps to the CUDA 12.4 builds of PyTorch and its companion libraries:

```bash
uv sync --extra cu124

```

The resolver reads the `[project.optional-dependencies]` table in [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml) to pull the correct platform-specific wheels from the index.

### 4. Verify the Installation with [`main.py`](https://github.com/baonguyen6742/uv-install-torch/blob/main/main.py)

Validate the stack by running the repository’s sanity-check script:

```bash
uv run python main.py

```

The script imports the scientific-Python stack, prints version metadata, and executes the `basic_calculation` function to confirm that CUDA tensors allocate successfully on the available device.

## Code Examples

The [`main.py`](https://github.com/baonguyen6742/uv-install-torch/blob/main/main.py) file contains a minimal validation routine that exercises PyTorch’s device placement:

```python

# Excerpt from main.py

import torch

def basic_calculation(a: int, b: int) -> torch.Tensor:
    device = "cuda" if torch.cuda.is_available() else "cpu"
    tensor_a = torch.arange(start=1, end=a + 1).unsqueeze(0)
    tensor_b = torch.arange(start=1, end=b + 1).unsqueeze(0)
    return torch.matmul(tensor_a.T, tensor_b).to(device)

print("Test calculation")
print(basic_calculation(2, 3))

```

A successful reinstall produces output indicating the tensor resides on `cuda:0` (or falls back to `cpu` on machines without a GPU):

```text
Test calculation
tensor([[1, 2, 3],
        [2, 4, 6]], device='cuda:0')

```

Automate the entire clean-reinstall workflow in a single shell block:

```bash

# Clean previous state

rm -rf .venv uv.lock

# Re-resolve dependencies (CUDA 12.4)

uv sync --extra cu124

# Verify installation

uv run python main.py

```

## Summary

* **`.venv`** holds compiled wheels; deleting it removes corrupted or mismatched CUDA binaries.
* **`uv.lock`** pins resolved versions; removing it forces a fresh dependency graph calculation.
* The `baonguyen6742/uv-install-torch` project uses the `cu124` extra in [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml) to select CUDA-enabled PyTorch builds.
* The `basic_calculation` function in [`main.py`](https://github.com/baonguyen6742/uv-install-torch/blob/main/main.py) provides a quick runtime verification that the new environment is functional.
* Optional cache cleaning via `uv cache clean` or `uv cache prune` eliminates bad wheel artifacts from previous attempts.

## Frequently Asked Questions

### What does `uv.lock` do in a uv project?

`uv.lock` is a lockfile that records the exact versions and platform-specific hashes of every dependency resolved during the last `uv sync` operation. It ensures reproducible builds across machines and time, but it can become stale if [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml) changes or if the initial resolution selected incompatible wheels. Deleting it triggers a full re-resolution from the current state of the package index.

### Is it safe to delete the `.venv` directory?

Yes. The `.venv` directory is a disposable virtual environment created by uv. It contains only installed packages and symlinks; no source code or irreplaceable data. You can safely delete it at any time and recreate it with `uv sync`, which will re-install all dependencies declared in [`pyproject.toml`](https://github.com/baonguyen6742/uv-install-torch/blob/main/pyproject.toml).

### How do I know if PyTorch is using CUDA after reinstalling?

Run the validation script (`uv run python main.py`) from the repository. The `basic_calculation` function checks `torch.cuda.is_available()` and moves the resulting tensor to the `"cuda"` device if possible. If the printed tensor shows `device='cuda:0'`, PyTorch is utilizing your NVIDIA GPU; if it shows `device='cpu'`, it is running on the CPU.

### When should I use `uv cache clean` versus `uv cache prune`?

Use **`uv cache prune`** to remove unused or orphaned cache entries globally without affecting packages that are currently installed in any project. Use **`uv cache clean <package>`** (e.g., `uv cache clean torch`) when you suspect a specific package’s wheel is corrupted and you want to force a fresh download of only that artifact during the next sync.