Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

19.4 KB
Raw Blame
"""
Delete API Routes

Provides endpoints for delete operations:
- Delete document
- Delete document blob only
- Delete documents in bulk
- Delete blobs in bulk
- Remove document from collection
- Delete collection
- Delete collection index only
"""

import json

from fastapi import APIRouter, Depends, Request
from fastapi.responses import JSONResponse
from ..dependencies.auth import require_auth
from ..dependencies.json_body import json_body_error
from ..dependencies.threadpool import run_db_sync

from ...research_library.utils import handle_api_error
from ...research_library.deletion.services.document_deletion import (
    DocumentDeletionService,
)
from ...research_library.deletion.services.collection_deletion import (
    CollectionDeletionService,
    PROTECTED_COLLECTION_TYPES,
)
from ...research_library.deletion.services.bulk_deletion import (
    BulkDeletionService,
)
from typing import Annotated

from .library import _MAX_STRING_LIST_ITEMS, _MAX_STRING_LIST_ITEM_LENGTH

router = APIRouter(prefix="/library/api", tags=["delete"])
# NOTE: Routes use username (not .get()) intentionally.
# Depends(require_auth) guarantees the key exists; direct access fails
# fast if the dependency is ever removed.


async def _parse_json_body(request: Request):
    """Parse the request body as JSON, returning a 400 on malformed bytes.

    ``await request.json()`` raises ``json.JSONDecodeError`` on a malformed
    body. The app registers a ``json.JSONDecodeError -> 400`` handler
    (fastapi_app.py), but every route below wraps its whole body in a broad
    ``except Exception -> handle_api_error(...)`` (a hardcoded 500) for
    genuine internal errors, which intercepts the decode error first and
    never lets it reach that handler. Parsing here, and converting the
    decode error to the same 400 shape the app-level handler would have
    produced, restores the correct status without touching that broad
    except (still needed for real internal errors).

    Returns:
        tuple: (data, error_response) where error_response is None on
               success, or a JSONResponse (400) on a malformed body.
    """
    try:
        data = await request.json()
    except json.JSONDecodeError:
        return None, json_body_error("success", "Invalid JSON body")
    return data, None


def _validate_document_ids_from_data(data):
    """Validate document_ids from an already-parsed JSON body.

    Returns:
        tuple: (document_ids, error_response) where error_response is None
               on success, or a JSONResponse on failure.
    """
    # ``isinstance(data, dict)`` (not just ``not data``) matters: a truthy
    # non-dict body (e.g. a bare int, or a bare string that happens to equal
    # "document_ids") reaches ``"document_ids" not in data`` /
    # ``data["document_ids"]`` below and raises TypeError for some inputs
    # (e.g. ``"document_ids" not in 42``) that the caller's broad
    # ``except Exception`` then turns into a 500.
    if not isinstance(data, dict) or "document_ids" not in data:
        return None, JSONResponse(
            {
                "success": False,
                "error": "document_ids required in request body",
            },
            status_code=400,
        )

    document_ids = data["document_ids"]
    # Document IDs are always UUID strings (see Document.id, String(36)) —
    # requiring str elements here, not just "is a non-empty list", also
    # closes off crashes downstream: a huge int overflows the sqlite3
    # driver's 64-bit bind and an unhashable element (list/dict) blows up
    # the per-document delete lock's dict key, both surfacing as 500s from
    # services this router does not own. Rejecting non-string elements here
    # keeps every hostile element type out of that code entirely.
    if not isinstance(document_ids, list) or not document_ids:
        return None, JSONResponse(
            {
                "success": False,
                "error": "document_ids must be a non-empty list",
            },
            status_code=400,
        )

    # Use the same manual-selection limits as the sibling library routes.
    # Check count before scanning elements: the 16 MB JSON body tier can
    # otherwise trigger hundreds of thousands of per-document operations.
    if len(document_ids) > _MAX_STRING_LIST_ITEMS:
        return None, JSONResponse(
            {
                "success": False,
                "error": (
                    "document_ids must contain at most"
                    f" {_MAX_STRING_LIST_ITEMS} items"
                ),
            },
            status_code=400,
        )

    if not all(isinstance(doc_id, str) for doc_id in document_ids):
        return None, JSONResponse(
            {
                "success": False,
                "error": "document_ids must be a non-empty list",
            },
            status_code=400,
        )

    if any(
        len(doc_id) > _MAX_STRING_LIST_ITEM_LENGTH for doc_id in document_ids
    ):
        return None, JSONResponse(
            {
                "success": False,
                "error": (
                    "document_ids items must be at most"
                    f" {_MAX_STRING_LIST_ITEM_LENGTH} characters"
                ),
            },
            status_code=400,
        )

    return document_ids, None


async def _validate_document_ids(request: Request):
    """Extract and validate document_ids from the JSON request body."""
    data, error = await _parse_json_body(request)
    if error:
        return None, error
    return _validate_document_ids_from_data(data)


# =============================================================================
# Document Delete Endpoints
# =============================================================================


@router.delete("/document/{document_id}")
def delete_document(
    request: Request,
    document_id,
    username: Annotated[str, Depends(require_auth)],
):
    """
    Delete a document and all related data.

    Tooltip: "Permanently delete this document, including PDF and text content.
              This cannot be undone."

    Returns:
        JSON with deletion details including chunks deleted, blob size freed
    """
    try:
        service = DocumentDeletionService(username)
        result = service.delete_document(document_id)

        if result.get("deleted"):
            return {"success": True, **result}
        # Note refusal (bypass guard): caller targeted a note via the
        # document API. Return 403 so the frontend can route them to
        # DELETE /api/notes/<id>, distinct from 404 (not found).
        if result.get("is_note"):
            return JSONResponse({"success": False, **result}, status_code=403)
        return JSONResponse({"success": False, **result}, status_code=404)

    except Exception as e:
        return handle_api_error("deleting document", e)


@router.delete("/document/{document_id}/blob")
def delete_document_blob(
    request: Request,
    document_id,
    username: Annotated[str, Depends(require_auth)],
):
    """
    Delete PDF binary but keep document metadata and text content.

    Tooltip: "Remove the PDF file to save space. Text content will be
              preserved for searching."

    Returns:
        JSON with bytes freed
    """
    try:
        service = DocumentDeletionService(username)
        result = service.delete_blob_only(document_id)

        if result.get("deleted"):
            return {"success": True, **result}
        # Note refusal: mirror the sibling DELETE /document/<id> route
        # which returns 403 when delete_document declines on a note.
        if result.get("is_note"):
            return JSONResponse({"success": False, **result}, status_code=403)
        error_code = (
            404 if "not found" in result.get("error", "").lower() else 400
        )
        # FastAPI does not honor Flask's ``(body, status)`` tuple return — it
        # would serialize the 2-tuple as an HTTP 200 with a ``[{...}, 404]``
        # array body. Use JSONResponse explicitly, matching the sibling
        # delete routes.
        return JSONResponse(
            {"success": False, **result}, status_code=error_code
        )

    except Exception as e:
        return handle_api_error("deleting document blob", e)


@router.get("/document/{document_id}/preview")
def get_document_deletion_preview(
    request: Request,
    document_id,
    username: Annotated[str, Depends(require_auth)],
):
    """
    Get a preview of what will be deleted.

    Returns information about the document to help user confirm deletion.
    """
    try:
        service = DocumentDeletionService(username)
        result = service.get_deletion_preview(document_id)

        if result.get("found"):
            return {"success": True, **result}
        return JSONResponse(
            {"success": False, "error": "Document not found"},
            status_code=404,
        )

    except Exception as e:
        return handle_api_error("getting document preview", e)


# =============================================================================
# Collection Document Endpoints
# =============================================================================


@router.delete("/collection/{collection_id}/document/{document_id}")
def remove_document_from_collection(
    request: Request,
    collection_id,
    document_id,
    username: Annotated[str, Depends(require_auth)],
):
    """
    Remove document from a collection.

    If the document is not in any other collection, it will be deleted.

    Tooltip: "Remove from this collection. If not in any other collection,
              the document will be deleted."

    Returns:
        JSON with unlink status and whether document was deleted
    """
    try:
        service = DocumentDeletionService(username)
        result = service.remove_from_collection(document_id, collection_id)

        if result.get("unlinked"):
            return {"success": True, **result}
        # Protected-home refusal (note in its Notes collection): 403 so
        # the frontend can distinguish it from 404 (not found / not in
        # this collection), mirroring the is_note guard on delete_document.
        if result.get("protected"):
            return JSONResponse({"success": False, **result}, status_code=403)
        return JSONResponse({"success": False, **result}, status_code=404)

    except Exception as e:
        return handle_api_error("removing document from collection", e)


# =============================================================================
# Collection Delete Endpoints
# =============================================================================


@router.delete("/collections/{collection_id}")
def delete_collection(
    request: Request,
    collection_id,
    username: Annotated[str, Depends(require_auth)],
):
    """
    Delete a collection and clean up all related data.

    Deletes the collection, its RAG index, chunks, and any orphaned
    documents (documents not in any other collection).

    Returns:
        JSON with deletion details including orphaned documents deleted
    """
    try:
        service = CollectionDeletionService(username)
        result = service.delete_collection(
            collection_id, delete_orphaned_documents=True
        )

        if result.get("deleted"):
            from ...web_search_engines.search_engines_config import (
                invalidate_collection_engines_cache,
            )

            invalidate_collection_engines_cache(username)
            return {"success": True, **result}
        error = result.get("error", "Unknown error")
        # 404 not found, 409 for a protected/system collection the service
        # refuses to delete, 503 when the vector store couldn't be reached
        # (the collection was not deleted; a retry can succeed), 500 when a
        # vector-store collection could not be dropped for a reason a retry
        # won't fix (misconfiguration, auth, no drop support), else 400.
        # FastAPI doesn't honor Flask's `(body, status)` tuple return, so use
        # JSONResponse explicitly. The 503/500 cases are keyed off
        # ``result["retryable"]`` / ``result["vector_store_error"]`` (set by
        # CollectionDeletionService when RemoteVectorStoreDropError
        # propagates out of CascadeHelper) rather than matching the "error"
        # text, so a wording change can't silently misroute a DELETE onto
        # 404 — which a naive retry/cleanup layer would read as "already
        # gone" and mark deleted, while the remote vectors and RAGIndex row
        # survive (see #5765).
        if result.get("retryable"):
            status_code = 503
        elif result.get("vector_store_error"):
            status_code = 500
        elif "not found" in error.lower():
            status_code = 404
        elif result.get("collection_type") in PROTECTED_COLLECTION_TYPES:
            status_code = 409
        else:
            status_code = 400
        return JSONResponse(
            {"success": False, **result}, status_code=status_code
        )

    except Exception as e:
        return handle_api_error("deleting collection", e)


@router.delete("/collections/{collection_id}/index")
def delete_collection_index(
    request: Request,
    collection_id,
    username: Annotated[str, Depends(require_auth)],
):
    """
    Delete only the RAG index for a collection, keeping the collection itself.

    Useful for rebuilding an index from scratch.

    Returns:
        JSON with deletion details
    """
    try:
        service = CollectionDeletionService(username)
        result = service.delete_collection_index_only(collection_id)

        if result.get("deleted"):
            return {"success": True, **result}
        # 503 when the vector store couldn't be reached (the index was not
        # deleted; a retry can succeed), 500 when a vector-store collection
        # could not be dropped for a reason a retry won't fix — see
        # delete_collection above for why this is keyed off the service's
        # machine-readable keys rather than the error text. 404 ONLY for a
        # genuinely missing collection; any other failure (the service's
        # generic "Failed to delete collection index" after a rollback) is
        # a 500, not a 404 a cleanup layer would read as "already gone".
        # Kept as literal JSONResponse(status_code=...) branches (rather
        # than one call fed by a variable) so every code stays visible to
        # tests/web/test_route_table_parity.py's AST scan, which only
        # credits a route with a status code when it sees a literal
        # `status_code=<int>` at the call site.
        if result.get("retryable"):
            return JSONResponse({"success": False, **result}, status_code=503)
        if result.get("vector_store_error"):
            return JSONResponse({"success": False, **result}, status_code=500)
        if "not found" in str(result.get("error", "")).lower():
            return JSONResponse({"success": False, **result}, status_code=404)
        return JSONResponse({"success": False, **result}, status_code=500)

    except Exception as e:
        return handle_api_error("deleting collection index", e)


@router.get("/collections/{collection_id}/preview")
def get_collection_deletion_preview(
    request: Request,
    collection_id,
    username: Annotated[str, Depends(require_auth)],
):
    """
    Get a preview of what will be deleted.

    Returns information about the collection to help user confirm deletion.
    """
    try:
        service = CollectionDeletionService(username)
        result = service.get_deletion_preview(collection_id)

        if result.get("found"):
            return {"success": True, **result}
        return JSONResponse(
            {"success": False, "error": "Collection not found"},
            status_code=404,
        )

    except Exception as e:
        return handle_api_error("getting collection preview", e)


# =============================================================================
# Bulk Delete Endpoints
# =============================================================================


@router.delete("/documents/bulk")
async def delete_documents_bulk(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """
    Delete multiple documents at once.

    Tooltip: "Permanently delete all selected documents and their associated data."

    Request body:
        {"document_ids": ["id1", "id2", ...]}

    Returns:
        JSON with bulk deletion results
    """
    try:
        document_ids, error = await _validate_document_ids(request)
        if error:
            return error

        # Per-document DB deletion loop — run on the threadpool so a
        # large selection cannot stall the event loop.
        result = await run_db_sync(
            lambda: BulkDeletionService(username).delete_documents(document_ids)
        )

        return {"success": True, **result}

    except Exception as e:
        return handle_api_error("bulk deleting documents", e)


@router.delete("/documents/blobs")
async def delete_documents_blobs_bulk(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """
    Delete PDF binaries for multiple documents.

    Tooltip: "Remove PDF files from selected documents to free up database space.
              Text content is preserved."

    Request body:
        {"document_ids": ["id1", "id2", ...]}

    Returns:
        JSON with bulk blob deletion results
    """
    try:
        document_ids, error = await _validate_document_ids(request)
        if error:
            return error

        result = await run_db_sync(
            lambda: BulkDeletionService(username).delete_blobs(document_ids)
        )

        return {"success": True, **result}

    except Exception as e:
        return handle_api_error("bulk deleting blobs", e)


@router.delete("/collection/{collection_id}/documents/bulk")
async def remove_documents_from_collection_bulk(
    request: Request,
    collection_id,
    username: Annotated[str, Depends(require_auth)],
):
    """
    Remove multiple documents from a collection.

    Documents that are not in any other collection will be deleted.

    Request body:
        {"document_ids": ["id1", "id2", ...]}

    Returns:
        JSON with bulk removal results
    """
    try:
        document_ids, error = await _validate_document_ids(request)
        if error:
            return error

        result = await run_db_sync(
            lambda: BulkDeletionService(
                username
            ).remove_documents_from_collection(document_ids, collection_id)
        )

        return {"success": True, **result}

    except Exception as e:
        return handle_api_error("bulk removing documents from collection", e)


@router.post("/documents/preview")
async def get_bulk_deletion_preview(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """
    Get a preview of what will be affected by a bulk operation.

    Request body:
        {
            "document_ids": ["id1", "id2", ...],
            "operation": "delete" or "delete_blobs"
        }

    Returns:
        JSON with preview information
    """
    try:
        data, error = await _parse_json_body(request)
        if error:
            return error
        document_ids, error = _validate_document_ids_from_data(data)
        if error:
            return error

        operation = data.get("operation", "delete")

        result = await run_db_sync(
            lambda: BulkDeletionService(username).get_bulk_preview(
                document_ids, operation
            )
        )

        return {"success": True, **result}

    except Exception as e:
        return handle_api_error("getting bulk preview", e)