# How to Perform a Clean Rebuild of the Code-Graph-RAG Index

> Learn how to perform a clean rebuild of the code-graph-rag index. Wipe the Memgraph database and local cache, then re-index your repository for a fresh code graph from scratch.

- Repository: [Vitali Avagyan/code-graph-rag](https://github.com/vitali87/code-graph-rag)
- Tags: how-to-guide
- Published: 2026-09-05

---

**A clean rebuild wipes the entire Memgraph database and local hash cache using the `--clean` flag, then re-indexes your repository with `--update-graph` to create a fresh code graph from scratch.**

Code-Graph-RAG stores every indexed project in a shared Memgraph graph database, which can drift out of sync with source code over time. According to the vitali87/code-graph-rag source code, a clean rebuild guarantees a completely fresh state by executing a full graph wipe followed by a complete re-indexing of the target repository.

## What Triggers a Clean Rebuild

The CLI implements the clean rebuild workflow through three logical steps reflected in [`codebase_rag/cli.py`](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/cli.py). When you invoke the `--clean` flag, the system:

1. Executes `MATCH (n) DETACH DELETE n` against Memgraph to delete every node and relationship
2. Clears the local hash cache that enables incremental indexing (defined in [`codebase_rag/constants/cli.py`](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/constants/cli.py) as `CLI_MSG_CLEANING_HASH_CACHE`)
3. Optionally re-indexes the target repository if `--update-graph` is also specified

This process is distinct from incremental updates because it removes **all projects** from the graph, not just the target repository.

## CLI Command Structure

The clean rebuild requires combining two specific flags: `--clean` to wipe the database and `--update-graph` to trigger the indexing pipeline.

### Basic Clean Rebuild Command

```bash
cgr start \
    --repo-path /path/to/my/project \
    --clean \
    --update-graph

```

In [`codebase_rag/cli.py`](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/cli.py), the `--clean` flag triggers the code path that prints `CLI_MSG_CLEANING_DB` and runs the Cypher deletion command. However, `--clean` alone only deletes data; it does **not** automatically start an indexing run. The `--update-graph` flag tells the CLI to invoke `GraphUpdater` from [`codebase_rag/graph_updater.py`](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/graph_updater.py), which walks the source tree, parses it with Tree-sitter, and populates the fresh graph.

### Handling the Confirmation Prompt

Because `--clean` deletes every project in the shared database, the CLI displays `CLI_WARN_CLEAN_OTHER_PROJECTS` and prompts for confirmation via `CLI_PROMPT_CLEAN_CONFIRM` when running interactively.

To skip the confirmation in headless environments or scripts, append the `--yes` flag:

```bash
cgr start \
    --repo-path /path/to/my/project \
    --clean \
    --update-graph \
    --yes

```

If you omit `--yes` in a non-interactive context, the CLI raises `CLI_ERR_CLEAN_NEEDS_CONFIRMATION` and exits to prevent accidental data loss.

## Programmatic Rebuild Using the Python SDK

You can reproduce the clean rebuild logic programmatically by interacting directly with the `GraphUpdater` class and Memgraph connection:

```python
from codebase_rag.graph_updater import GraphUpdater
from codebase_rag.constants.cli import CLI_MSG_CLEANING_DB

# Step 1: Wipe the entire graph

print(CLI_MSG_CLEANING_DB)
updater = GraphUpdater()
updater.memgraph.run("MATCH (n) DETACH DELETE n")

# Step 2: Re-index the repository

updater.run(
    repo_path="/path/to/my/project",
    update_graph=True,
)

```

This approach bypasses the CLI confirmation logic, so ensure you handle deletion safeguards in your application code when targeting production databases.

## Key Differences Between Flags

Understanding the interaction between flags prevents incomplete rebuilds:

- **`--clean`**: Executes the Cypher wipe and clears hash caches (defined in [`codebase_rag/constants/cli.py`](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/constants/cli.py)). Deletes **all** projects from Memgraph.
- **`--update-graph`**: Triggers the `GraphUpdater.run()` method to parse and index the specified `--repo-path`.
- **`--yes`**: Suppresses the interactive confirmation required when `--clean` detects other projects in the database.

Running `--clean` without `--update-graph` leaves you with an empty database. Running `--update-graph` without `--clean` performs an incremental update, preserving existing graph data.

## Summary

- A **clean rebuild** requires both `--clean` and `--update-graph` flags to first wipe then rebuild the index
- The `--clean` flag executes `MATCH (n) DETACH DELETE n` in Memgraph and clears the hash cache via `CLI_MSG_CLEANING_HASH_CACHE`
- Always use `--yes` in CI/CD pipelines to bypass the `CLI_PROMPT_CLEAN_CONFIRM` interactive check
- The `GraphUpdater` class in [`codebase_rag/graph_updater.py`](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/graph_updater.py) handles the actual Tree-sitter parsing and graph population
- Clean rebuilds affect **all projects** in the shared database, not just the target repository

## Frequently Asked Questions

### What is the difference between the `--clean` and `--update-graph` flags?

The `--clean` flag triggers a deletion of the entire Memgraph database and local hash cache, as implemented in [`codebase_rag/cli.py`](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/cli.py), but does not index any code. The `--update-graph` flag invokes the `GraphUpdater` to parse source files and populate the graph. For a complete rebuild, you must use both flags together.

### Will a clean rebuild delete other indexed projects stored in the database?

Yes. Because Code-Graph-RAG uses a shared Memgraph instance, the `--clean` flag executes `MATCH (n) DETACH DELETE n`, which removes **every node and relationship** across all projects. The CLI warns you via `CLI_WARN_CLEAN_OTHER_PROJECTS` before proceeding unless you specify `--yes`.

### How do I automate clean rebuilds in CI/CD pipelines?

Include the `--yes` flag to suppress the interactive confirmation prompt (`CLI_PROMPT_CLEAN_CONFIRM`). The command `cgr start --repo-path . --clean --update-graph --yes` runs deterministically in headless environments without waiting for user input.

### What happens to the incremental indexing cache during a clean rebuild?

The clean rebuild process clears the local hash cache that tracks previously indexed file states. As defined in [`codebase_rag/constants/cli.py`](https://github.com/vitali87/code-graph-rag/blob/main/codebase_rag/constants/cli.py), the CLI emits `CLI_MSG_CLEANING_HASH_CACHE` when this occurs, forcing the next indexing operation to process every file from scratch rather than skipping unchanged files.