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.

43.4 KB
Raw Blame
"""
Document deletion service.

Handles:
- Full document deletion with proper cascade cleanup
- Blob-only deletion (remove PDF, keep text)
- Remove from collection (unlink or delete if orphaned)
"""

import threading
from contextlib import contextmanager
from typing import Any, Dict, Iterator, List, Tuple

from loguru import logger
from sqlalchemy import exists

from ....constants import FILE_PATH_BLOB_DELETED
from ....database.models.library import (
    Collection,
    Document,
    DocumentChunk,
    DocumentCollection,
    RagDocumentStatus,
    SourceType,
)
from ....database.session_context import get_user_db_session
from ....database.session_passwords import capture_request_db_password
from ....utilities.formatting import short_id
from ..utils.cascade_helper import CascadeHelper
from ...services.pdf_storage_manager import effective_storage_mode


# Document deletion is a read-then-delete workflow with filesystem and FAISS
# side effects around the database transaction. Serialize it per user/document
# across service instances so duplicate browser requests cannot both pass the
# initial existence check. The registry is ref-counted so completed document
# IDs do not accumulate for the lifetime of the process.
_document_delete_locks: Dict[Tuple[str, str], threading.Lock] = {}
_document_delete_lock_refs: Dict[Tuple[str, str], int] = {}
_document_delete_locks_guard = threading.Lock()


@contextmanager
def _hold_document_delete_lock(
    username: str, document_id: str
) -> Iterator[None]:
    """Hold the process-local lock for one user's document deletion.

    The lock is intentionally non-reentrant. Callers must not start a nested
    deletion for the same user and document while this context is active.
    """
    key = (username, document_id)
    with _document_delete_locks_guard:
        lock = _document_delete_locks.get(key)
        if lock is None:
            lock = threading.Lock()
            _document_delete_locks[key] = lock
        _document_delete_lock_refs[key] = (
            _document_delete_lock_refs.get(key, 0) + 1
        )

    try:
        with lock:
            yield
    finally:
        with _document_delete_locks_guard:
            remaining = _document_delete_lock_refs[key] - 1
            if remaining:
                _document_delete_lock_refs[key] = remaining
            else:
                _document_delete_lock_refs.pop(key, None)
                # Identity check protects against future registry maintenance
                # replacing the lock while this holder is winding down.
                if _document_delete_locks.get(key) is lock:
                    _document_delete_locks.pop(key, None)


def _is_note_document(session, document) -> bool:
    """Return True if the document represents a note (source_type='note').

    Notes have their own deletion API at /api/notes/<id>. Letting them be
    deleted via the generic document-deletion endpoints would bypass the
    B1 protection that was specifically built to prevent note data loss
    (see PROTECTED_COLLECTION_TYPES in collection_deletion.py).

    The check is local-by-design — keep this module free of imports from
    the notes package to avoid an import cycle.
    """
    note_source = session.query(SourceType).filter_by(name="note").first()
    return bool(note_source) and document.source_type_id == note_source.id


class DocumentDeletionService:
    """Service for document deletion operations."""

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

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

    def purge_deleted_research_documents(
        self,
        targets: List[Tuple[str, List[str]]],
        db_password: str | None = None,
    ) -> None:
        """Purge vectors and residual chunks after research deletion commits."""
        groups: Dict[str, set[str]] = {}
        deleted_documents: set[str] = set()
        for document_id, collection_ids in targets:
            # The pre-delete snapshot also contains documents transferred to
            # surviving research by the cascade. Only a missing document may
            # lose its RAG data; query committed state, not the old ownership.
            try:
                session_context = (
                    get_user_db_session(self.username, db_password)
                    if db_password is not None
                    else get_user_db_session(self.username)
                )
                with session_context as session:
                    if (
                        session.query(Document.id)
                        .filter_by(id=document_id)
                        .first()
                        is not None
                    ):
                        continue
            except Exception:
                logger.warning(
                    f"Could not verify research document removal; "
                    f"skipping RAG cleanup for document "
                    f"{short_id(document_id)} "
                    f"({len(collection_ids)} collection"
                    f"{'s' if len(collection_ids) != 1 else ''})"
                )
                continue
            deleted_documents.add(document_id)
            for collection_id in collection_ids:
                groups.setdefault(collection_id, set()).add(document_id)

        from ...services.rag_service_factory import get_rag_service

        for collection_id, document_ids in groups.items():
            try:
                with get_rag_service(
                    self.username,
                    collection_id=collection_id,
                    db_password=db_password
                    if db_password is not None
                    else capture_request_db_password(self.username),
                ) as rag:
                    rag.purge_documents_vectors(
                        sorted(document_ids), collection_id
                    )
            except Exception:
                logger.opt(exception=True).warning(
                    "Deferred research vector cleanup"
                )
        # Preserve the deletion privacy backstop even if a vector file was
        # unavailable. Its unresolvable vectors are collected on reconciliation.
        if deleted_documents:
            try:
                session_context = (
                    get_user_db_session(self.username, db_password)
                    if db_password is not None
                    else get_user_db_session(self.username)
                )
                with session_context as session:
                    for document_id in deleted_documents:
                        CascadeHelper.delete_document_chunks(
                            session, document_id
                        )
                    session.commit()
            except Exception:
                logger.opt(exception=True).warning(
                    "Could not sweep deleted research chunks"
                )

    def reconcile_orphaned_document_chunks(
        self, db_password: str, batch_size: int = 25
    ) -> int:
        """Retry cleanup for deleted documents left behind by an interrupted purge.

        Bound the scan by document ID, then include every collection for each
        selected document before the existing purge sweeps all of its chunks.
        A later login handles any remaining documents in the next batch.
        """
        with get_user_db_session(self.username, db_password) as session:
            orphan_ids = [
                document_id
                for (document_id,) in session.query(DocumentChunk.source_id)
                .filter(
                    DocumentChunk.source_type == "document",
                    DocumentChunk.source_id.is_not(None),
                    ~exists().where(Document.id == DocumentChunk.source_id),
                )
                .distinct()
                .order_by(DocumentChunk.source_id)
                .limit(batch_size)
                .all()
            ]
            if not orphan_ids:
                return 0
            chunk_locations = (
                session.query(
                    DocumentChunk.source_id, DocumentChunk.collection_name
                )
                .filter(
                    DocumentChunk.source_type == "document",
                    DocumentChunk.source_id.in_(orphan_ids),
                )
                .distinct()
                .all()
            )

        collections: Dict[str, set[str]] = {
            document_id: set() for document_id in orphan_ids
        }
        for document_id, collection_name in chunk_locations:
            if collection_name.startswith("collection_"):
                collections[document_id].add(
                    collection_name.removeprefix("collection_")
                )
        self.purge_deleted_research_documents(
            [
                (document_id, sorted(collections[document_id]))
                for document_id in orphan_ids
            ],
            db_password=db_password,
        )
        return len(orphan_ids)

    def delete_document(self, document_id: str) -> Dict[str, Any]:
        """Delete one document, serializing duplicate in-process requests."""
        with _hold_document_delete_lock(self.username, document_id):
            result, rag_collection_ids = self._delete_document_locked(
                document_id
            )

        # Post-commit FAISS + chunk-row cleanup. Runs after the DB transaction
        # has committed AND the keyed lock is released, allowing concurrent
        # requests to immediately observe "Document not found" without waiting
        # for slow vector-store IO.
        if result.get("deleted"):
            self._purge_document_rag(
                document_id, rag_collection_ids, full_delete=True
            )

        return result

    def _delete_document_locked(
        self, document_id: str
    ) -> Tuple[Dict[str, Any], List[str]]:
        """Delete a document and all related data while its keyed lock is held.

        This method ensures complete DB/file cleanup:
        - DocumentChunks (no FK constraint, manual cleanup required)
        - DocumentBlob (CASCADE handles, but we track for stats)
        - Filesystem files
        - DownloadTracker update
        - DocumentCollection links (CASCADE)
        - RagDocumentStatus (CASCADE)

        Args:
            document_id: ID of the document to delete

        Returns:
            Tuple of (result_dict, rag_collection_ids) for post-commit purge.
        """
        result: Dict[str, Any] = {
            "deleted": False,
            "document_id": document_id,
            "title": None,
            "blob_deleted": False,
            "blob_size": 0,
            "chunks_deleted": 0,
            "collections_unlinked": 0,
            "file_deleted": False,
        }
        rag_collection_ids: List[str] = []

        with get_user_db_session(self.username) as session:
            try:
                # Get document
                document = session.get(Document, document_id)
                if not document:
                    return {
                        "deleted": False,
                        "document_id": document_id,
                        "error": "Document not found",
                    }, []

                # Notes are not deletable via the generic document API.
                # The Notes-collection deletion guard only blocks the
                # cascade path; without this check a user could wipe an
                # individual note by passing its UUID here, and the bulk
                # endpoint amplifies the same gap. Route the caller to
                # DELETE /api/notes/<id> which goes through NoteService
                # with the proper version/link cleanup.
                if _is_note_document(session, document):
                    return {
                        "deleted": False,
                        "document_id": document_id,
                        "title": document.title or "Untitled",
                        "error": (
                            "Notes cannot be deleted via the document API. "
                            "Use DELETE /api/notes/<id> instead."
                        ),
                        "is_note": True,
                    }, []

                title = document.title or document.filename or "Untitled"
                result["title"] = title

                # 1. Get collections before deletion for chunk cleanup
                collections = CascadeHelper.get_document_collections(
                    session, document_id
                )
                result["collections_unlinked"] = len(collections)

                # Every collection that may hold FAISS vectors / chunk rows
                # for this document: the UNION of its linked collections and
                # the collections that actually have chunk rows. The chunk-row
                # half matters because a partial/failed reindex can leave
                # chunks in a collection whose join row is gone or was never
                # marked indexed. Used by the post-commit RAG purge below.
                chunk_collection_rows = (
                    session.query(DocumentChunk.collection_name)
                    .filter_by(source_type="document", source_id=document_id)
                    .distinct()
                    .all()
                )
                rag_collection_ids = list(
                    {str(c) for c in collections}
                    | {
                        row[0].removeprefix("collection_")
                        for row in chunk_collection_rows
                        if row[0]
                    }
                )

                # 2. Count chunks for stats. The rows themselves (and their
                #    FAISS vectors) are removed in the post-commit RAG cleanup
                #    phase below — purge_document_vectors resolves the FAISS
                #    int ids from these rows, so they must outlive this
                #    transaction (mirrors NoteService.delete_note).
                result["chunks_deleted"] = (
                    session.query(DocumentChunk)
                    .filter(
                        DocumentChunk.source_id == document_id,
                        DocumentChunk.source_type == "document",
                    )
                    .count()
                )

                # 3. Get blob size before deletion (for stats)
                result["blob_size"] = CascadeHelper.get_document_blob_size(
                    session, document_id
                )
                result["blob_deleted"] = result["blob_size"] > 0

                # 4. Delete filesystem file if exists
                if (
                    effective_storage_mode(document) == "filesystem"
                    and document.file_path
                ):
                    from ...utils import (
                        get_absolute_path_from_settings,
                        get_containment_root_from_settings,
                    )

                    try:
                        # Re-check the unlink against the directory the
                        # resolver confines file_path to: this user's root,
                        # or <root>/pdfs for a stored pdfs/... path. The bare
                        # library root would refuse every PDF in the
                        # sanctioned layout that symlinks pdfs onto a larger
                        # disk, and orphan it there. The boundary is resolved
                        # (pinned) BEFORE the file is, and the re-check does
                        # not resolve it again (allowed_root_is_resolved), so
                        # a pdfs directory swapped for a symlink after this
                        # point moves only the file, which is then refused
                        # (#5481). The boundary is None only when the root
                        # lookup or its resolution fails.
                        # _resolved_containment_ok() treats allowed_root=None
                        # as "no boundary enforced" for callers that can't
                        # cheaply supply a root (e.g. the FAISS sweeps),
                        # which is fine there but would be a silent fail-open
                        # here. Fail closed instead of unlinking with no
                        # containment boundary.
                        allowed_root = get_containment_root_from_settings(
                            document.file_path,
                            self.username,
                        )
                        # Delete only within this user's own root — never the
                        # legacy shared root, where a colliding relative path
                        # can resolve to another tenant's file (#5521).
                        file_path = get_absolute_path_from_settings(
                            document.file_path,
                            self.username,
                        )
                        if file_path and allowed_root is not None:
                            result["file_deleted"] = (
                                CascadeHelper.delete_filesystem_file(
                                    str(file_path),
                                    allowed_root=allowed_root,
                                    allowed_root_is_resolved=True,
                                )
                            )
                        elif file_path:
                            logger.error(
                                "Refusing to delete filesystem file: "
                                "could not resolve library root for "
                                "containment check"
                            )
                    except Exception:
                        logger.exception("Failed to delete filesystem file")

                # 5. Update DownloadTracker
                CascadeHelper.update_download_tracker(session, document)

                # 6. Delete the document and all related records (leaves the
                #    DocumentChunk rows for the post-commit cleanup phase).
                #    The helper's row count is authoritative: a concurrent
                #    cross-process request can still win after our initial read
                #    because the keyed lock is intentionally process-local.
                #    Never report success or purge RAG state when this DELETE
                #    affected no document row.
                actually_deleted = CascadeHelper.delete_document_completely(
                    session, document_id
                )
                if not actually_deleted:
                    # Bind the acting user so the record survives the
                    # database sink's owner resolution and reaches the
                    # user's app_logs — without it the race signal is
                    # stderr-only (#5581). The loser just gets an ordinary
                    # "not found", so this line is the only trace of the
                    # race. The row has no research_id, so neither the
                    # research-history cascade nor any retention job ever
                    # removes it: bind only a rare event like this one,
                    # not per-delete success lines or failures that can
                    # repeat for every document of a bulk run.
                    logger.bind(username=self.username).info(
                        f"Document {short_id(document_id)} was already removed "
                        "by a concurrent delete request"
                    )
                    session.rollback()
                    return {
                        "deleted": False,
                        "document_id": document_id,
                        "error": "Document not found",
                    }, []

                session.commit()

                result["deleted"] = True
                logger.info(
                    f"Deleted document {short_id(document_id)} ({title}): "
                    f"{result['chunks_deleted']} chunks, "
                    f"{result['blob_size']} bytes blob"
                )
                return result, rag_collection_ids

            except Exception:
                logger.exception(f"Failed to delete document {document_id}")
                session.rollback()
                return {
                    "deleted": False,
                    "document_id": document_id,
                    "error": "Failed to delete document",
                }, []

    def _purge_document_rag(
        self,
        document_id: str,
        collection_ids: List[str],
        *,
        full_delete: bool,
    ) -> None:
        """Remove a document's FAISS vectors and chunk rows post-commit.

        Called after the deletion/unlink transaction has committed —
        releasing SQLite's single write lock — so the slow vector-store IO
        here never blocks another writer. For each collection the vectors
        are purged BEFORE their chunk rows (``purge_document_vectors``
        resolves the FAISS int ids from those rows), then the rows are
        deleted.

        A final sweep then guarantees no chunk row survives even when the
        per-collection purge could not run — e.g. the DB password is
        unavailable on an encrypted-DB install, or a collection has no
        current FAISS index. ``full_delete`` selects the sweep scope: True
        (the document is gone) sweeps every remaining chunk row for the
        document; False (a single unlink) sweeps only the listed
        collections, leaving the document's chunks in its other collections
        intact.

        Best-effort: the DB delete already committed, so failures here are
        logged, not surfaced.
        """
        if collection_ids:
            for collection_id in collection_ids:
                try:
                    dbpw = capture_request_db_password(self.username)
                    from ...services.rag_service_factory import (
                        get_rag_service,
                    )

                    # A fresh service per collection so the factory resolves
                    # THAT collection's stored embedding model/provider — a
                    # shared instance would compute the wrong index hash and
                    # purge nothing for non-default-embedding collections.
                    with get_rag_service(
                        self.username,
                        collection_id=collection_id,
                        db_password=dbpw,
                    ) as rag:
                        rag.purge_document_vectors(document_id, collection_id)
                        rag.purge_document_chunks(document_id, collection_id)
                except Exception:
                    # exception=True keeps the stack trace. dbpw is a live
                    # local here, but frame-local values only render under
                    # the opt-in LDR_LOGURU_DIAGNOSE dev flag — every
                    # persistent sink sets diagnose=False — so the password
                    # cannot reach a log in normal operation.
                    logger.opt(exception=True).error(
                        "Failed to remove RAG entries for document {} "
                        "from collection {}",
                        document_id,
                        collection_id,
                    )

        # Guaranteed backstop: remove any chunk rows the RAG purge could not
        # reach. Their vectors can't be resolved without the rows, but the
        # collection-scoped rehydration filter already stops an orphaned
        # vector from ever surfacing another document's text, so dropping the
        # rows here keeps the deletion complete and safe.
        try:
            with get_user_db_session(self.username) as session:
                if full_delete:
                    CascadeHelper.delete_document_chunks(session, document_id)
                else:
                    for collection_id in collection_ids:
                        CascadeHelper.delete_document_chunks(
                            session,
                            document_id,
                            f"collection_{collection_id}",
                        )
                session.commit()
        except Exception:
            logger.opt(exception=True).error(
                "Failed to sweep residual chunk rows for document {}",
                document_id,
            )

    def delete_blob_only(self, document_id: str) -> Dict[str, Any]:
        """
        Delete PDF binary but keep document metadata and text content.

        This saves database space while preserving searchability.

        Args:
            document_id: ID of the document

        Returns:
            Dict with deletion details:
            {
                "deleted": True/False,
                "document_id": str,
                "bytes_freed": int,
                "storage_mode_updated": bool,
                "error": str (if failed)
            }
        """
        with get_user_db_session(self.username) as session:
            try:
                # Get document
                document = session.get(Document, document_id)
                if not document:
                    return {
                        "deleted": False,
                        "document_id": document_id,
                        "bytes_freed": 0,
                        "error": "Document not found",
                    }

                # Notes are not mutable via the generic blob-delete API.
                # Without this guard, a note's storage_mode/file_path can
                # be overwritten with the PDF-blob-deletion sentinel via
                # either DELETE /library/api/document/<id>/blob OR the
                # bulk DELETE /library/api/documents/blobs path (which
                # loops over this method per-id).
                if _is_note_document(session, document):
                    return {
                        "deleted": False,
                        "document_id": document_id,
                        "bytes_freed": 0,
                        "error": (
                            "Notes cannot be modified via the document API. "
                            "Use the notes API instead."
                        ),
                        "is_note": True,
                    }

                result = {
                    "deleted": False,
                    "document_id": document_id,
                    "bytes_freed": 0,
                    "storage_mode_updated": False,
                }

                # Handle based on storage mode. Documents that predate the
                # column have it NULL, so infer it like PDFStorageManager.
                storage_mode = effective_storage_mode(document)
                if storage_mode == "database":
                    # Delete blob from database
                    result["bytes_freed"] = CascadeHelper.delete_document_blob(
                        session, document_id
                    )

                elif storage_mode == "filesystem":
                    # Delete filesystem file
                    from ...utils import (
                        get_absolute_path_from_settings,
                        get_containment_root_from_settings,
                    )

                    if document.file_path:
                        try:
                            # See the boundary and fail-closed comment on the
                            # equivalent guard in _delete_document_locked()
                            # above: the boundary is pinned before the file
                            # is resolved, and a destructive unlink must not
                            # silently fall back to a boundary-less
                            # containment check if it is ever None.
                            allowed_root = get_containment_root_from_settings(
                                document.file_path,
                                self.username,
                            )
                            # Own-root only: never unlink via the legacy
                            # shared root (cross-tenant collision, #5521).
                            file_path = get_absolute_path_from_settings(
                                document.file_path,
                                self.username,
                            )
                            if (
                                file_path
                                and file_path.is_file()
                                and allowed_root is not None
                            ):
                                result["bytes_freed"] = file_path.stat().st_size
                                CascadeHelper.delete_filesystem_file(
                                    str(file_path),
                                    allowed_root=allowed_root,
                                    allowed_root_is_resolved=True,
                                )
                            elif file_path and file_path.is_file():
                                logger.error(
                                    "Refusing to delete filesystem file: "
                                    "could not resolve library root for "
                                    "containment check"
                                )
                        except Exception:
                            logger.exception("Failed to delete filesystem file")

                else:
                    # No blob to delete
                    return {
                        "deleted": False,
                        "document_id": document_id,
                        "bytes_freed": 0,
                        "error": "Document has no stored PDF (storage_mode is 'none')",
                    }

                # Update document to indicate blob is deleted
                document.storage_mode = "none"
                document.file_path = FILE_PATH_BLOB_DELETED
                result["storage_mode_updated"] = True

                session.commit()
                result["deleted"] = True

                logger.info(
                    f"Deleted blob for document {short_id(document_id)}: "
                    f"{result['bytes_freed']} bytes freed"
                )

                return result

            except Exception:
                logger.exception(
                    f"Failed to delete blob for document {document_id}"
                )
                session.rollback()
                return {
                    "deleted": False,
                    "document_id": document_id,
                    "bytes_freed": 0,
                    "error": "Failed to delete document blob",
                }

    def remove_from_collection(
        self,
        document_id: str,
        collection_id: str,
    ) -> Dict[str, Any]:
        """
        Remove document from a collection.

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

        Args:
            document_id: ID of the document
            collection_id: ID of the collection

        Returns:
            Dict with operation details:
            {
                "unlinked": True/False,
                "document_deleted": bool,
                "document_id": str,
                "collection_id": str,
                "chunks_deleted": int,
                "error": str (if failed)
            }
        """
        with get_user_db_session(self.username) as session:
            try:
                # Verify document exists
                document = session.get(Document, document_id)
                if not document:
                    return {
                        "unlinked": False,
                        "document_deleted": False,
                        "document_id": document_id,
                        "collection_id": collection_id,
                        "error": "Document not found",
                    }

                # Verify collection exists and document is in it
                doc_collection = (
                    session.query(DocumentCollection)
                    .filter_by(
                        document_id=document_id, collection_id=collection_id
                    )
                    .first()
                )

                if not doc_collection:
                    return {
                        "unlinked": False,
                        "document_deleted": False,
                        "document_id": document_id,
                        "collection_id": collection_id,
                        "error": "Document not in this collection",
                    }

                # Permanent-home guard: the Notes collection is every
                # note's home (_get_or_create_notes_collection); nothing
                # re-links a note after an unlink, so removal here would
                # silently drop the note from semantic search, the
                # collection view, and the auto-reindex worker — while
                # list_notes still shows it. NoteService.remove_from_collection
                # enforces this for the notes API; this mirrors it for the
                # generic collection-document API (single + bulk).
                collection = session.get(Collection, collection_id)
                if (
                    collection is not None
                    and collection.collection_type == "notes"
                    and _is_note_document(session, document)
                ):
                    return {
                        "unlinked": False,
                        "document_deleted": False,
                        "document_id": document_id,
                        "collection_id": collection_id,
                        "protected": True,
                        "error": (
                            "Cannot remove a note from its notes "
                            "collection — it is the note's permanent "
                            "home. Use DELETE /api/notes/<id> to delete "
                            "the note itself."
                        ),
                    }

                result = {
                    "unlinked": False,
                    "document_deleted": False,
                    "document_id": document_id,
                    "collection_id": collection_id,
                    "chunks_deleted": 0,
                }

                # Count chunks for this document in this collection (for
                # stats). Their rows and FAISS vectors are removed in the
                # post-commit RAG cleanup phase — purge_document_vectors
                # resolves the FAISS int ids from these rows, so they must
                # outlive this transaction.
                collection_name = f"collection_{collection_id}"
                result["chunks_deleted"] = (
                    session.query(DocumentChunk)
                    .filter(
                        DocumentChunk.source_id == document_id,
                        DocumentChunk.source_type == "document",
                        DocumentChunk.collection_name == collection_name,
                    )
                    .count()
                )

                # Remove the link
                session.delete(doc_collection)
                # Clear the RagDocumentStatus row for this (document, collection)
                # — the "indexed" marker read by get_rag_stats / the RAG status
                # route. Its FK cascades fire only when the Document, Collection,
                # or RAGIndex is deleted; a mere unlink deletes none of those, so
                # a leftover row reports the just-removed document as still
                # indexed in this collection.
                session.query(RagDocumentStatus).filter_by(
                    document_id=document_id, collection_id=collection_id
                ).delete(synchronize_session=False)
                session.flush()

                # Check if document is in any other collection
                remaining_count = CascadeHelper.count_document_in_collections(
                    session, document_id
                )

                # Default (unlink, and note-orphan): purge only THIS
                # collection's vectors/chunks; the document (or note) and its
                # chunks in other collections stay intact.
                full_delete = False
                rag_collection_ids: List[str] = [collection_id]

                if remaining_count == 0 and _is_note_document(
                    session, document
                ):
                    # Note that was orphaned by this removal: skip the
                    # destructive delete_document_completely path. Notes
                    # live behind their own deletion API (DELETE /api/notes/<id>)
                    # and are still discoverable via list_notes() which
                    # filters by source_type_id, not collection membership.
                    # The Notes collection itself acts as the persistent
                    # home for every note via _get_or_create_notes_collection.
                    logger.info(
                        f"Note {short_id(document_id)} orphaned from collection — "
                        "skipping orphan delete; use DELETE /api/notes/<id> to remove."
                    )
                elif remaining_count == 0:
                    # Document is orphaned - delete it completely
                    # Note: We're already in a session, so we need to do this
                    # directly rather than calling delete_document()
                    logger.info(
                        f"Document {short_id(document_id)} is orphaned, deleting"
                    )

                    # Full delete: purge vectors/chunks across EVERY collection
                    # that still holds chunk rows for this document, not just
                    # the one being removed (a partial reindex can strand
                    # chunks elsewhere). Rows are deleted in the post-commit
                    # phase so purge_document_vectors can resolve their ids.
                    chunk_collection_rows = (
                        session.query(DocumentChunk.collection_name)
                        .filter_by(
                            source_type="document", source_id=document_id
                        )
                        .distinct()
                        .all()
                    )
                    full_delete = True
                    rag_collection_ids = list(
                        {collection_id}
                        | {
                            row[0].removeprefix("collection_")
                            for row in chunk_collection_rows
                            if row[0]
                        }
                    )

                    # Update DownloadTracker
                    CascadeHelper.update_download_tracker(session, document)

                    # Delete filesystem file if applicable
                    if (
                        effective_storage_mode(document) == "filesystem"
                        and document.file_path
                    ):
                        from ...utils import (
                            get_absolute_path_from_settings,
                            get_containment_root_from_settings,
                        )

                        try:
                            # See the boundary and fail-closed comment on the
                            # equivalent guard in _delete_document_locked()
                            # above: the boundary is pinned before the file
                            # is resolved, and a destructive unlink must not
                            # silently fall back to a boundary-less
                            # containment check if it is ever None.
                            allowed_root = get_containment_root_from_settings(
                                document.file_path,
                                self.username,
                            )
                            # Own-root only: never unlink via the legacy
                            # shared root (cross-tenant collision, #5521).
                            file_path = get_absolute_path_from_settings(
                                document.file_path,
                                self.username,
                            )
                            if file_path and allowed_root is not None:
                                CascadeHelper.delete_filesystem_file(
                                    str(file_path),
                                    allowed_root=allowed_root,
                                    allowed_root_is_resolved=True,
                                )
                            elif file_path:
                                logger.error(
                                    "Refusing to delete filesystem file: "
                                    "could not resolve library root for "
                                    "containment check"
                                )
                        except Exception:
                            logger.exception("Failed to delete filesystem file")

                    # Delete document and all related records (leaves the
                    # DocumentChunk rows for the post-commit cleanup phase).
                    CascadeHelper.delete_document_completely(
                        session, document_id
                    )
                    result["document_deleted"] = True

                session.commit()
                result["unlinked"] = True

                logger.info(
                    f"Removed document {short_id(document_id)} from collection "
                    f"{short_id(collection_id)} "
                    f"(deleted={result['document_deleted']})"
                )

            except Exception:
                logger.exception(
                    f"Failed to remove document {document_id} "
                    f"from collection {collection_id}"
                )
                session.rollback()
                return {
                    "unlinked": False,
                    "document_deleted": False,
                    "document_id": document_id,
                    "collection_id": collection_id,
                    "error": "Failed to remove document from collection",
                }

        # Post-commit FAISS + chunk-row cleanup, after the transaction has
        # committed and released SQLite's write lock.
        self._purge_document_rag(
            document_id, rag_collection_ids, full_delete=full_delete
        )

        return result

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

        Useful for showing the user what will happen before confirming.

        Args:
            document_id: ID of the document

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

            collections = CascadeHelper.get_document_collections(
                session, document_id
            )

            # Count chunks
            total_chunks = (
                session.query(DocumentChunk)
                .filter(
                    DocumentChunk.source_id == document_id,
                    DocumentChunk.source_type == "document",
                )
                .count()
            )

            blob_size = CascadeHelper.get_document_blob_size(
                session, document_id
            )

            return {
                "found": True,
                "document_id": document_id,
                "title": document.title or document.filename or "Untitled",
                "file_type": document.file_type,
                "storage_mode": document.storage_mode,
                "has_blob": blob_size > 0,
                "blob_size": blob_size,
                "has_text": bool(document.text_content),
                "collections_count": len(collections),
                "chunks_count": total_chunks,
            }