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.

26.0 KB
Raw Blame
"""
Collection deletion service.

Handles:
- Full collection deletion with proper cleanup
- Documents are preserved but unlinked
- RAG index and chunks are deleted
"""

from typing import Dict, List, Any

from loguru import logger

from ....database.models.library import (
    Collection,
    Document,
    DocumentCollection,
    DocumentChunk,
    CollectionFolder,
    RAGIndex,
    RagDocumentStatus,
    SourceType,
)
from ....database.session_context import get_user_db_session
from ....utilities.formatting import short_id
from ..utils.cascade_helper import (
    CascadeHelper,
    RemoteVectorStoreDropError,
    read_vector_cleanup_settings_snapshot,
    remote_drop_failure_message,
)

# System collection types that must never be deleted via the public API.
# These collections hold first-class user data whose lifecycle is owned by
# other subsystems (Library, Research History, Notes); allowing them to be
# deleted via the generic collection-delete path triggers cascade orphan
# deletion of every document inside, which for Notes means total data loss
# of notes, versions, links, and synthesis sources.
# NOTE: 'zotero' is deliberately NOT protected — unlike the singleton system
# collections above, users create/remove Zotero-synced collections and the
# generic delete path is their only UI for it. Whether deleting a Zotero
# collection should also purge its synced documents is a separate question
# for the Zotero sync owner to decide, not something to block wholesale here.
PROTECTED_COLLECTION_TYPES = frozenset(
    {"default_library", "research_history", "notes"}
)


def _user_settings_snapshot(session) -> Dict[str, Any]:
    """The user's settings snapshot, read through the deletion's own session.

    The RAG-index cascade needs it to resolve the user's vector-store provider
    and its connection config (#5765); without it the provider silently falls
    back to the global default. Read through ``session`` (the caller's own
    per-user session), so this is safe off the request thread.

    Strictly read-only: ``seed_defaults=False`` because the default
    constructor seeds an EMPTY settings table from the defaults files and
    COMMITS on ``session`` -- mid-deletion, outside the deletion's own
    rollback. An empty table still reads as all-defaults (the manager merges
    its in-memory defaults). Fails closed on an unreadable snapshot once a
    server-backed store exists (see read_vector_cleanup_settings_snapshot).
    """
    from ....settings.manager import SettingsManager

    return read_vector_cleanup_settings_snapshot(
        SettingsManager(session, seed_defaults=False)
    )


def _remove_unfinished_rebuilds(username: str, collection_id: str) -> None:
    """Remove interrupted rebuild stages; the deletion has already committed.

    A failure only leaves those stages behind, so it is logged without a
    traceback rather than reported as a failed deletion.
    """
    from ...services.collection_rebuild import remove_unfinished_rebuilds

    try:
        remove_unfinished_rebuilds(
            username, collection_id, lambda: get_user_db_session(username)
        )
    except Exception:
        logger.warning(
            "Could not remove unfinished rebuilds of collection {}",
            short_id(collection_id),
        )


def _remote_drop_failure_result(
    collection_id: str, exc: RemoteVectorStoreDropError, outcome: str
) -> Dict[str, Any]:
    """Result dict for a deletion aborted by a failed remote drop."""
    return {
        "deleted": False,
        "collection_id": collection_id,
        "error": remote_drop_failure_message(exc, outcome),
        # Stable machine-readable keys so callers (the router) map this shape
        # to a status code without pattern matching the human-readable
        # "error" text (#5765): ``vector_store_error`` marks the shape and
        # ``retryable`` says whether a retry can succeed (503) or not (500).
        "vector_store_error": True,
        "retryable": exc.retryable,
    }


class CollectionDeletionService:
    """Service for collection deletion operations."""

    def __init__(self, username: str):
        """
        Initialize collection deletion service.

        Args:
            username: Username for database session
        """
        self.username = username

    def delete_collection(
        self, collection_id: str, delete_orphaned_documents: bool = True
    ) -> Dict[str, Any]:
        """
        Delete a collection and clean up all related data.

        By default, orphaned documents (not in any other collection) are deleted.
        Set delete_orphaned_documents=False to preserve all documents.

        The following are deleted:
        - DocumentChunks for this collection
        - Vector data (FAISS index files, or the provider's remote
          collection — dropped before the RAGIndex rows, see #5765)
        - RAGIndex records
        - CollectionFolder records (CASCADE)
        - DocumentCollection links (CASCADE)
        - RagDocumentStatus records (CASCADE)
        - Orphaned documents (if delete_orphaned_documents=True)

        Args:
            collection_id: ID of the collection to delete
            delete_orphaned_documents: If True, delete documents not in any
                other collection after unlinking

        Returns:
            Dict with deletion details:
            {
                "deleted": True/False,
                "collection_id": str,
                "collection_name": str,
                "chunks_deleted": int,
                "documents_unlinked": int,
                "indices_deleted": int,
                "folders_deleted": int,
                "orphaned_documents_deleted": int,
                "error": str (if failed)
            }
        """
        with get_user_db_session(self.username) as session:
            try:
                # Get collection
                collection = session.get(Collection, collection_id)
                if not collection:
                    return {
                        "deleted": False,
                        "collection_id": collection_id,
                        "error": "Collection not found",
                    }

                if collection.collection_type in PROTECTED_COLLECTION_TYPES:
                    logger.warning(
                        "Refused to delete protected system collection "
                        f"{short_id(collection_id)} (type={collection.collection_type})"
                    )
                    return {
                        "deleted": False,
                        "collection_id": collection_id,
                        "collection_name": collection.name,
                        "collection_type": collection.collection_type,
                        "error": (
                            "Cannot delete system collection "
                            f"'{collection.name}' (type={collection.collection_type}). "
                            "This collection holds first-class user data."
                        ),
                    }

                collection_name = f"collection_{collection_id}"
                result = {
                    "deleted": False,
                    "collection_id": collection_id,
                    "collection_name": collection.name,
                    "chunks_deleted": 0,
                    "documents_unlinked": 0,
                    "indices_deleted": 0,
                    "folders_deleted": 0,
                    "orphaned_documents_deleted": 0,
                }

                # 1. Get document IDs BEFORE deleting links (for orphan check)
                doc_ids_in_collection = [
                    dc.document_id
                    for dc in session.query(DocumentCollection)
                    .filter_by(collection_id=collection_id)
                    .all()
                ]
                result["documents_unlinked"] = len(doc_ids_in_collection)

                # 2. Delete RAGIndex records (DB only). The on-disk FAISS files
                #    are unlinked AFTER the commit below — steps 3-8 still do DB
                #    work that could raise and roll back, and a rollback restores
                #    the RAGIndex rows but not files already deleted from disk.
                #
                #    A SERVER-BACKED store is the opposite case and is handled
                #    inside the helper: its remote collection is dropped BEFORE
                #    the rows go, because the collection name is derived from
                #    RAGIndex.index_hash. Deferring that to post-commit like the
                #    files would leave the vectors unaddressable (#5765). A
                #    failed drop raises RemoteVectorStoreDropError, caught below.
                #
                #    This runs BEFORE the first write of this transaction (the
                #    chunk delete below) so a remote drop's network call is not
                #    made while holding the user's database write lock. The
                #    user's settings snapshot selects the provider and carries
                #    its connection config.
                rag_result = CascadeHelper.delete_rag_indices_for_collection(
                    session,
                    collection_name,
                    unlink_files=False,
                    username=self.username,
                    settings_snapshot=_user_settings_snapshot(session),
                )
                result["indices_deleted"] = rag_result["deleted_indices"]
                faiss_paths_to_unlink = rag_result["index_paths"]

                # 3. Delete DocumentChunks for this collection
                result["chunks_deleted"] = (
                    CascadeHelper.delete_collection_chunks(
                        session, collection_name
                    )
                )
                # Orphaned documents hard-deleted below may still have chunk
                # rows/vectors in OTHER collections (a partial reindex can strand
                # them). Collect them here and purge those after the commit —
                # delete_document_completely does not touch DocumentChunk.
                orphaned_doc_ids: List[str] = []

                # 4. Count folders before deletion
                result["folders_deleted"] = (
                    session.query(CollectionFolder)
                    .filter_by(collection_id=collection_id)
                    .count()
                )

                # 5. Delete DocumentCollection links explicitly before collection
                session.query(DocumentCollection).filter_by(
                    collection_id=collection_id
                ).delete(synchronize_session=False)

                # 6. Delete linked folders explicitly
                session.query(CollectionFolder).filter_by(
                    collection_id=collection_id
                ).delete(synchronize_session=False)

                # 7. Delete the collection itself
                session.delete(collection)

                # 8. Delete orphaned documents if requested
                if delete_orphaned_documents:
                    # Notes must never be hard-deleted via the orphan
                    # cascade: they live behind their own deletion API
                    # (DELETE /api/notes/<id>) and remain discoverable
                    # via list_notes(), which filters by source_type_id
                    # rather than collection membership. This mirrors the
                    # note-skip in document_deletion.py's orphan path. The
                    # check is kept local (a single SourceType lookup) to
                    # avoid importing the notes-services package into the
                    # deletion package, matching document_deletion's own
                    # local _is_note_document.
                    note_source = (
                        session.query(SourceType).filter_by(name="note").first()
                    )
                    for doc_id in doc_ids_in_collection:
                        # Check if document is in any other collection
                        remaining = (
                            session.query(DocumentCollection)
                            .filter_by(document_id=doc_id)
                            .count()
                        )
                        if remaining == 0:
                            if note_source is not None:
                                document = session.get(Document, doc_id)
                                if (
                                    document is not None
                                    and document.source_type_id
                                    == note_source.id
                                ):
                                    logger.info(
                                        f"Note {short_id(doc_id)} orphaned by "
                                        "collection deletion — skipping orphan "
                                        "delete; use DELETE /api/notes/<id> "
                                        "to remove."
                                    )
                                    continue
                            # Document is orphaned - delete it
                            CascadeHelper.delete_document_completely(
                                session, doc_id
                            )
                            orphaned_doc_ids.append(doc_id)
                            result["orphaned_documents_deleted"] += 1
                            logger.info(
                                f"Deleted orphaned document {short_id(doc_id)}"
                            )

                session.commit()

                from ....web_search_engines.search_engines_config import (
                    invalidate_collection_engines_cache,
                )

                invalidate_collection_engines_cache(self.username)

                result["deleted"] = True
                logger.info(
                    f"Deleted collection {short_id(collection_id)} "
                    f"({result['collection_name']}): {result['chunks_deleted']} chunks, "
                    f"{result['documents_unlinked']} documents unlinked, "
                    f"{result['orphaned_documents_deleted']} orphaned deleted"
                )

            except RemoteVectorStoreDropError as exc:
                # A remote collection could not be dropped, so its vectors are
                # still there. Roll back so the RAGIndex rows — the only handle
                # to those vectors — survive, and report the deletion as
                # FAILED. Completing it here would be the one outcome that
                # cannot be undone or retried (#5765). Collections dropped
                # before the failing one stay dropped (exc.dropped); the
                # message says so rather than claiming nothing changed.
                #
                # Logged from the error's sanitized fields, at WARNING and
                # with no traceback: a traceback (logger.exception) would
                # add frame state, and the backend client's error text can
                # carry a URI or token.
                drop_detail = exc.log_detail()
                logger.warning(
                    f"Aborted deletion of collection {collection_id}: could "
                    f"not drop its vector-store collection: {drop_detail}"
                )
                session.rollback()
                return _remote_drop_failure_result(
                    collection_id, exc, "the collection was not deleted"
                )
            except Exception:
                logger.exception(f"Failed to delete collection {collection_id}")
                session.rollback()
                return {
                    "deleted": False,
                    "collection_id": collection_id,
                    "error": "Failed to delete collection",
                }

        # Post-commit best-effort cleanup, OUTSIDE the deletion transaction and
        # its try/except: the DB delete is already durable, so a failure here
        # must NOT be reported as a failed collection delete — it isn't one. It
        # would only leave orphaned files/vectors, logged for ops visibility.
        if faiss_paths_to_unlink:
            from ....config.paths import get_cache_directory

            # NOTE: allowed_root is the COARSE shared rag_indices/ root, not
            # the tighter per-user rag_indices/<sha256(user)>/ subdir. It
            # cannot be narrowed to the per-user subdir without risking
            # refused deletes: pre-per-user-scoping (legacy) indexes live
            # DIRECTLY in this shared root (see
            # library_rag_service._migrate_legacy_index_files), so a per-user
            # allowed_root would reject a legacy-layout RAGIndex.index_path
            # and orphan its files. The containment check still blocks
            # symlink / ancestor escapes outside rag_indices/ entirely.
            rag_indices_root = get_cache_directory() / "rag_indices"
            for faiss_path in faiss_paths_to_unlink:
                CascadeHelper.delete_faiss_index_files(
                    faiss_path, allowed_root=rag_indices_root
                )

        # Before the orphan purge below, so it never sees staging chunk rows.
        _remove_unfinished_rebuilds(self.username, collection_id)

        # Purge each hard-deleted orphan's vectors + chunk rows in its OTHER
        # collections (this collection's chunks were removed in step 2). Reuses
        # DocumentDeletionService's per-collection vector purge + chunk backstop.
        if orphaned_doc_ids:
            from .document_deletion import DocumentDeletionService
            from ....database.models.library import DocumentChunk

            deleter = DocumentDeletionService(self.username)
            for doc_id in orphaned_doc_ids:
                try:
                    with get_user_db_session(self.username) as purge_session:
                        coll_rows = (
                            purge_session.query(DocumentChunk.collection_name)
                            .filter_by(source_type="document", source_id=doc_id)
                            .distinct()
                            .all()
                        )
                    collection_ids = [
                        row[0].removeprefix("collection_")
                        for row in coll_rows
                        if row[0]
                    ]
                    deleter._purge_document_rag(
                        doc_id, collection_ids, full_delete=True
                    )
                except Exception:
                    logger.opt(exception=True).error(
                        "Failed to purge orphaned document {} after collection "
                        "delete (delete already committed)",
                        doc_id,
                    )

        return result

    def delete_collection_index_only(
        self, collection_id: str
    ) -> Dict[str, Any]:
        """
        Delete only the RAG index for a collection, keeping the collection itself.

        This is useful for rebuilding an index from scratch.

        Args:
            collection_id: ID of the collection

        Returns:
            Dict with deletion details
        """
        with get_user_db_session(self.username) as session:
            try:
                # Verify collection exists
                collection = session.get(Collection, collection_id)
                if not collection:
                    return {
                        "deleted": False,
                        "collection_id": collection_id,
                        "error": "Collection not found",
                    }

                collection_name = f"collection_{collection_id}"
                result = {
                    "deleted": False,
                    "collection_id": collection_id,
                    "chunks_deleted": 0,
                    "indices_deleted": 0,
                    "documents_reset": 0,
                }

                # 1. Delete RAGIndex records (DB only). Files are unlinked after
                #    the commit — steps 2-5 do more DB work that could roll back,
                #    and a rollback can't restore files already removed from disk.
                #    A server-backed store's remote collection is dropped inside
                #    the helper BEFORE the rows go, and before this
                #    transaction's first write (see delete_collection).
                rag_result = CascadeHelper.delete_rag_indices_for_collection(
                    session,
                    collection_name,
                    unlink_files=False,
                    username=self.username,
                    settings_snapshot=_user_settings_snapshot(session),
                )
                result["indices_deleted"] = rag_result["deleted_indices"]
                faiss_paths_to_unlink = rag_result["index_paths"]

                # 2. Delete DocumentChunks
                result["chunks_deleted"] = (
                    CascadeHelper.delete_collection_chunks(
                        session, collection_name
                    )
                )

                # 3. Reset DocumentCollection indexed status
                result["documents_reset"] = (
                    session.query(DocumentCollection)
                    .filter_by(collection_id=collection_id)
                    .update({"indexed": False, "chunk_count": 0})
                )

                # 4. Delete RagDocumentStatus for this collection
                session.query(RagDocumentStatus).filter_by(
                    collection_id=collection_id
                ).delete(synchronize_session=False)

                # 5. Reset collection embedding info
                collection.embedding_model = None
                collection.embedding_model_type = None
                collection.embedding_dimension = None
                collection.chunk_size = None
                collection.chunk_overlap = None
                collection.rebuild_pending_since = None

                session.commit()

                # DB delete durable — now unlink the FAISS files (best-effort).
                if faiss_paths_to_unlink:
                    from ....config.paths import get_cache_directory

                    # NOTE: allowed_root is the COARSE shared rag_indices/
                    # root, not the per-user rag_indices/<sha256(user)>/
                    # subdir -- see delete_collection above for why it can't
                    # be narrowed (legacy indexes live directly in the shared
                    # root, so a per-user scope would orphan them).
                    rag_indices_root = get_cache_directory() / "rag_indices"
                    for faiss_path in faiss_paths_to_unlink:
                        CascadeHelper.delete_faiss_index_files(
                            faiss_path, allowed_root=rag_indices_root
                        )

                result["deleted"] = True

                logger.info(
                    f"Deleted index for collection {short_id(collection_id)}: "
                    f"{result['chunks_deleted']} chunks, "
                    f"{result['documents_reset']} documents reset"
                )

            except RemoteVectorStoreDropError as exc:
                # See delete_collection: keep the RAGIndex rows so the remote
                # collection stays addressable, and report failure. Logged
                # the same way (sanitized fields, no traceback).
                drop_detail = exc.log_detail()
                logger.warning(
                    f"Aborted index deletion for collection {collection_id}: "
                    f"could not drop its vector-store collection: {drop_detail}"
                )
                session.rollback()
                return _remote_drop_failure_result(
                    collection_id, exc, "the index was not deleted"
                )
            except Exception:
                logger.exception(
                    f"Failed to delete index for collection {collection_id}"
                )
                session.rollback()
                return {
                    "deleted": False,
                    "collection_id": collection_id,
                    "error": "Failed to delete collection index",
                }

        # After the commit and outside its scope, as in delete_collection.
        _remove_unfinished_rebuilds(self.username, collection_id)
        return result

    def get_deletion_preview(self, collection_id: str) -> Dict[str, Any]:
        """
        Get a preview of what will be deleted.

        Useful for showing the user what will happen before confirming.

        Args:
            collection_id: ID of the collection

        Returns:
            Dict with preview information
        """
        with get_user_db_session(self.username) as session:
            collection = session.get(Collection, collection_id)
            if not collection:
                return {"found": False, "collection_id": collection_id}

            collection_name = f"collection_{collection_id}"

            # Count documents
            documents_count = (
                session.query(DocumentCollection)
                .filter_by(collection_id=collection_id)
                .count()
            )

            # Count chunks
            chunks_count = (
                session.query(DocumentChunk)
                .filter_by(collection_name=collection_name)
                .count()
            )

            # Count folders
            folders_count = (
                session.query(CollectionFolder)
                .filter_by(collection_id=collection_id)
                .count()
            )

            # Check for RAG index
            has_index = (
                session.query(RAGIndex)
                .filter_by(collection_name=collection_name)
                .first()
                is not None
            )

            return {
                "found": True,
                "collection_id": collection_id,
                "name": collection.name,
                "description": collection.description,
                "is_default": collection.is_default,
                "documents_count": documents_count,
                "chunks_count": chunks_count,
                "folders_count": folders_count,
                "has_rag_index": has_index,
                "embedding_model": collection.embedding_model,
            }