~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
Cascade helper for deletion operations.
Handles cleanup of related records that don't have proper FK constraints:
- DocumentChunk (source_id has no FK constraint)
- Local vector-index files (FAISS)
- Server-backed vector collections (dropped before their RAGIndex row goes)
- Filesystem files
"""
import time
from pathlib import Path
from typing import Any, Dict, Iterable, List, NamedTuple, Optional, Type, Union
from loguru import logger
from sqlalchemy.orm import Session
from ....constants import FILE_PATH_SENTINELS
from ....database.models.library import (
Document,
DocumentBlob,
DocumentChunk,
DocumentCollection,
RAGIndex,
)
from ....utilities.formatting import short_id
# A <name>.*.tmp younger than this is assumed to be a concurrent writer's live
# persist() temp (not yet os.replace()d into place); deleting it would crash
# that write. Matches FaissVectorStore.persist()'s own stale-temp sweep so both
# paths agree on when a temp is certainly crash-orphaned rather than in-flight.
_STALE_TMP_AGE_SECONDS = 3600
class RemoteVectorStoreDropError(RuntimeError):
"""A server-backed vector collection could not be dropped.
Raised by :meth:`CascadeHelper.delete_rag_indices_for_collection` when a
``RAGIndex`` row belongs to a server-backed (not local-file) store and its
collection could not be dropped.
This is deliberately an exception rather than a logged warning: it is
raised BEFORE any ``RAGIndex`` row is deleted, so it aborts the caller's
transaction and leaves ``index_hash`` -- the only handle by which the
remote collection can be addressed -- intact. Reporting the deletion as
successful here is what turns retained vectors into *unrecoverable*
vectors (issue #5765).
The message is fixed text plus the provider key, an ``index_hash`` prefix
and the *type name* of the underlying error -- never that error's own
text, which for a network client can carry a backend URI or credential.
For the same reason the underlying error is not chained (``from None``),
and callers log :meth:`log_detail` without a traceback.
Attributes:
retryable: True only when the failure is known to be transient: the
backend could not be reached or timed out (``ConnectionError`` /
``TimeoutError``, or an error carrying ``retryable = True`` -- see
:meth:`BaseVectorStore.drop_collection`). False for failures a
retry cannot fix: no username, a row with no ``index_hash``, an
unknown provider, a store with no drop implementation, an
authentication/permission error, or anything unclassified.
dropped: how many remote collections this call had ALREADY dropped
(or found absent) before the failure. Remote drops cannot be made
all-or-nothing: when this is non-zero those collections are gone
although the caller's rollback restores their ``RAGIndex`` rows
(and the collection's ``indexed`` flags), so search over them
returns nothing until the operation is retried -- ``drop_collection``
is idempotent, so a retried delete completes -- or the collection
is force re-indexed. A normal (non-force) re-index does NOT
rebuild them, because the restored rows still read as indexed.
cause_type: type name of the underlying error, if there was one.
"""
def __init__(
self,
message: str,
*,
retryable: bool = False,
dropped: int = 0,
cause_type: Optional[str] = None,
) -> None:
super().__init__(message)
self.retryable = retryable
self.dropped = dropped
self.cause_type = cause_type
def log_detail(self) -> str:
"""A log line for this failure built only from sanitized fields.
Callers log this -- at WARNING, with no traceback -- instead of the
exception itself: the message is fixed text plus a provider key, an
``index_hash`` prefix and a type name, and nothing else about the
underlying error is recorded.
"""
return (
f"{self.args[0]} (retryable={self.retryable}, "
f"already dropped={self.dropped})"
)
def _is_transient_drop_failure(exc: BaseException) -> bool:
"""Whether a ``drop_collection`` failure is worth retrying.
An explicit boolean ``retryable`` attribute on the error wins, so a
backend can classify its own client's exceptions. Otherwise only the
built-in transport failures count as transient; everything else
(``PermissionError``, ``NotImplementedError``, auth errors, unknown
exceptions) is reported as permanent rather than inviting an endless
retry loop.
"""
flag = getattr(exc, "retryable", None)
if isinstance(flag, bool):
return flag
return isinstance(exc, (ConnectionError, TimeoutError))
def remote_drop_failure_message(
exc: RemoteVectorStoreDropError, outcome: str
) -> str:
"""User-facing text for a failed remote drop.
``outcome`` states what did NOT happen, e.g. ``"the collection was not
deleted"``. The text is built only from fixed phrases and counts, never
from the underlying error.
"""
if exc.retryable:
message = (
f"Vector store unreachable — {outcome}. Retry once the vector "
"store is available."
)
else:
message = (
f"The vector store could not drop the collection — {outcome}. "
"Retrying will not help until the vector store configuration or "
"permissions are fixed; see the server log."
)
if exc.dropped:
message += (
f" {exc.dropped} vector-store collection(s) had already been "
"removed before the failure; their index records were kept, so "
"search over them returns nothing until this is retried "
"successfully or the collection is force re-indexed."
)
return message
def read_vector_cleanup_settings_snapshot(settings_manager: Any) -> dict:
"""The user's settings snapshot for a vector cleanup, failing closed.
The cascade resolves each row's provider -- and a server-backed store's
connection config -- from this snapshot (#5765). A non-strict
``get_all_settings()`` answers a failed settings query with a
defaults-only snapshot, which would silently select the global default
provider and connection config: the drop would miss (or target the wrong
store), be counted as "already absent", and the rows -- the only handle to
the real collection -- would then be deleted.
So the read is strict. On failure:
* while only local-file providers exist
(:func:`~local_deep_research.vector_stores.config.only_local_file_providers_registered`),
the snapshot cannot change the cleanup (every row's files are unlinked,
nothing is dropped), so this degrades to the non-strict read instead of
failing a deletion that would otherwise succeed;
* otherwise it raises :class:`RemoteVectorStoreDropError` (not retryable)
BEFORE any row is touched, so callers abort exactly as for a failed
drop.
``settings_manager`` is only read from. The caller chooses how it is
built; a caller inside a transaction must use one that does not seed and
commit defaults (``SettingsManager(session, seed_defaults=False)``).
"""
from sqlalchemy.exc import SQLAlchemyError
from ....vector_stores import config as vs_config
try:
return settings_manager.get_all_settings(strict=True)
except (SQLAlchemyError, LookupError) as exc:
cause_type = type(exc).__name__
if vs_config.only_local_file_providers_registered():
logger.warning(
"Could not read the user's settings ({}); continuing the vector "
"cleanup with defaults because only local-file vector stores "
"exist",
cause_type,
)
return settings_manager.get_all_settings()
raise RemoteVectorStoreDropError(
"Could not read the user's settings "
f"({cause_type}), so the vector-store provider and its connection "
"config are unknown; refusing to delete the RAGIndex records that "
"hold the only handle to their vectors.",
retryable=False,
cause_type=cause_type,
)
class _PlannedIndex(NamedTuple):
"""One ``RAGIndex`` row with its vector cleanup decided up front."""
row: RAGIndex
provider: str
# None for a local-file store (unlink its files); the store class for a
# server-backed one (drop its collection).
remote_store: Optional[Type[Any]]
class CascadeHelper:
"""Helper class for cleaning up related records during deletion."""
@staticmethod
def delete_document_chunks(
session: Session,
document_id: str,
collection_name: Optional[str] = None,
) -> int:
"""
Delete DocumentChunks for a document.
Since DocumentChunk.source_id has no FK constraint, we must manually
clean up chunks when deleting a document.
Args:
session: Database session
document_id: The document ID to delete chunks for
collection_name: Optional collection name to limit deletion scope
Returns:
Number of chunks deleted
"""
query = session.query(DocumentChunk).filter(
DocumentChunk.source_id == document_id,
DocumentChunk.source_type == "document",
)
if collection_name:
query = query.filter(
DocumentChunk.collection_name == collection_name
)
count = query.delete(synchronize_session=False)
logger.debug(
f"Deleted {count} chunks for document {short_id(document_id)}"
+ (f" in collection {collection_name}" if collection_name else "")
)
return count
@staticmethod
def delete_collection_chunks(
session: Session,
collection_name: str,
) -> int:
"""
Delete all DocumentChunks for a collection.
Args:
session: Database session
collection_name: The collection name (e.g., "collection_<uuid>")
Returns:
Number of chunks deleted
"""
count = (
session.query(DocumentChunk)
.filter_by(collection_name=collection_name)
.delete(synchronize_session=False)
)
logger.debug(f"Deleted {count} chunks for collection {collection_name}")
return count
@staticmethod
def get_document_blob_size(session: Session, document_id: str) -> int:
"""
Get the size of a document's blob in bytes.
Args:
session: Database session
document_id: The document ID
Returns:
Size in bytes, or 0 if no blob exists
"""
blob = (
session.query(DocumentBlob)
.filter_by(document_id=document_id)
.first()
)
if blob and blob.pdf_binary:
return len(blob.pdf_binary)
return 0
@staticmethod
def delete_document_blob(session: Session, document_id: str) -> int:
"""
Delete a document's blob record.
Note: This is typically handled by CASCADE, but can be called explicitly
for blob-only deletion.
Args:
session: Database session
document_id: The document ID
Returns:
Size of deleted blob in bytes
"""
blob = (
session.query(DocumentBlob)
.filter_by(document_id=document_id)
.first()
)
if blob:
size = len(blob.pdf_binary) if blob.pdf_binary else 0
session.delete(blob)
logger.debug(
f"Deleted blob for document {short_id(document_id)} ({size} bytes)"
)
return size
return 0
@staticmethod
def _resolved_containment_ok(
path: Path,
allowed_root: Optional[Union[str, Path]],
*,
allowed_root_is_resolved: bool = False,
) -> bool:
"""Return True if `path`'s fully-resolved real location stays under
`allowed_root`.
Resolution follows every symlink in the chain -- not just the
final component -- so this also catches an escape via a symlinked
*ancestor* directory (e.g. a library subfolder swapped for a symlink
pointing outside the library root between validation and delete).
It uses `resolve_allow_missing()`, never a bare non-strict
`resolve()`: that one ignores ENAMETOOLONG, so a symlink chain
whose resolved form outgrows PATH_MAX could hide an escape.
`allowed_root` is resolved here too, unless the caller passes
`allowed_root_is_resolved=True`. In that case it was already
resolved before the path was validated, and it is compared as
given. Resolving it again would let a boundary directory swapped
for a symlink after validation move the boundary along with the
file.
When `allowed_root` is None, no containment boundary is enforced
here; callers that can't cheaply supply a root still get the
unconditional is_symlink() check the callers of this helper perform
on the leaf file itself.
"""
if allowed_root is None:
return True
from ....security.path_validator import resolve_allow_missing
try:
resolved_path = resolve_allow_missing(path)
if allowed_root_is_resolved:
resolved_root = Path(allowed_root)
else:
resolved_root = resolve_allow_missing(Path(allowed_root))
except OSError:
# Can't resolve (e.g. a broken component) -- refuse rather than
# risk unlinking something outside the intended root.
return False
return resolved_path.is_relative_to(resolved_root)
@staticmethod
def delete_filesystem_file(
file_path: Optional[str],
allowed_root: Optional[Union[str, Path]] = None,
*,
allowed_root_is_resolved: bool = False,
) -> bool:
"""
Delete a file from the filesystem.
Args:
file_path: Path to the file (can be relative or absolute)
allowed_root: If provided, the file's fully-resolved real path
must be contained within this directory or the delete is
refused. Defense-in-depth against a symlink planted at (or
swapped in for) `file_path` redirecting the unlink to an
arbitrary file elsewhere on disk -- see #5481.
allowed_root_is_resolved: True when `allowed_root` was resolved
before `file_path` was validated, as
`get_containment_root_from_settings` returns it. It is then
compared as given instead of being resolved again, so a
boundary directory swapped for a symlink since then cannot
move the boundary along with the file.
Returns:
True if file was deleted, False otherwise
"""
if not file_path:
return False
# Skip special path markers
if file_path in FILE_PATH_SENTINELS:
return False
try:
path = Path(file_path)
# Refuse to unlink through a symlink. This check is unconditional
# (doesn't require allowed_root) -- is_file()/unlink() both
# follow symlinks, so without it a symlink planted at this exact
# path would redirect the delete to whatever it points at.
if path.is_symlink():
logger.warning(
f"Refusing to delete symlinked filesystem file: {file_path}"
)
return False
if not CascadeHelper._resolved_containment_ok(
path,
allowed_root,
allowed_root_is_resolved=allowed_root_is_resolved,
):
logger.warning(
f"Refusing to delete filesystem file outside allowed root: {file_path}"
)
return False
if path.is_file():
# nosemgrep: semgrep.rules.path-traversal-risk, reason: Leaf symlinks are refused and resolved containment is checked above; document callers require an allowed root
path.unlink()
logger.debug(f"Deleted filesystem file: {file_path}")
return True
except Exception:
logger.exception(f"Failed to delete filesystem file: {file_path}")
return False
@staticmethod
def delete_faiss_index_files(
index_path: Optional[str],
allowed_root: Optional[Union[str, Path]] = None,
) -> bool:
"""
Delete FAISS index files.
FAISS stores indices as .faiss and .pkl files.
Args:
index_path: Path to the FAISS index file (without extension)
allowed_root: If provided, the resolved index family's parent
directory must be contained within this directory or the
delete is refused. See `delete_filesystem_file` for the
symlink-escape rationale.
Returns:
True if the family was fully deleted, False if nothing was
deleted or some component refused cleanup (e.g. a foreign child
in the segments directory) — every removable member is still
removed in that case, but the incomplete cleanup is reported.
"""
if not index_path:
return False
try:
path = Path(index_path)
if not CascadeHelper._resolved_containment_ok(path, allowed_root):
logger.warning(
f"Refusing to delete FAISS index outside allowed root: {index_path}"
)
return False
deleted_any = False
cleanup_incomplete = False
from ....vector_stores.segment_files import (
checkpoint_path,
delete_segment_files,
)
checkpoint = checkpoint_path(path.with_suffix(".faiss"))
faiss_file = path.with_suffix(".faiss")
pkl_file = path.with_suffix(".pkl")
idmap_file = path.with_suffix(".idmap.json")
for member, label in (
(checkpoint, "rollback checkpoint"),
(faiss_file, "index file"),
(pkl_file, "pkl file"),
(idmap_file, "idmap sidecar"),
):
try:
if member.is_symlink():
cleanup_incomplete = True
logger.warning(
f"Refusing to delete symlinked FAISS {label}: {member}"
)
elif member.is_file():
# nosemgrep: semgrep.rules.path-traversal-risk, reason: Each fixed-suffix family member derives from the root-checked index base and symlink leaves are refused above
member.unlink()
logger.debug(f"Deleted FAISS {label}: {member}")
deleted_any = True
except FileNotFoundError:
continue
except OSError:
cleanup_incomplete = True
logger.warning(f"Could not remove FAISS {label}: {member}")
# Segments go only AFTER the manifest root, never leaving a
# manifest pointing at deleted segments (a later write at this
# path would refuse it and quarantine the collection). A root
# that could not be removed keeps its segments.
# Contained per component: one foreign or unexpected child in the
# segments directory (e.g. a .DS_Store dropped by a file manager
# into a directory docs/library-and-rag.md tells users to back up)
# must not abort removal of the rest of the family — especially
# the plaintext .pkl above. delete_segment_files still refuses to
# delete anything it does not recognize. The refusal is still
# REPORTED: the family delete returns False (cleanup incomplete)
# even though every removable member is removed.
# Fail closed: a root whose state cannot be read (Path.exists()
# re-raises EACCES/ENAMETOOLONG on Python < 3.14) keeps its
# segments and must not abort the quarantine sweep below.
try:
root_present = faiss_file.is_symlink() or faiss_file.exists()
except (OSError, ValueError):
root_present = True
if root_present:
cleanup_incomplete = True
else:
try:
if delete_segment_files(faiss_file):
deleted_any = True
except (OSError, ValueError):
cleanup_incomplete = True
logger.warning(
f"Could not fully retire vector segment files for "
f"{index_path}"
)
# Sweep crash-orphaned temp files (persist writes <name>.*.tmp) and
# quarantined siblings (<name>.corrupt-<ns>) for the whole family,
# so deleting a collection doesn't leave them on disk forever — a
# leftover .pkl.corrupt-* is quarantined PLAINTEXT.
parent = path.parent
now = time.time()
for base in (faiss_file, pkl_file, idmap_file):
# .tmp files are AGE-GATED: FaissVectorStore.persist() writes to
# a <name>.*.tmp then os.replace()s it into place, so a live temp
# is a concurrent writer's not-yet-renamed file — unlinking it
# would crash that persist with FileNotFoundError. Only sweep
# temps old enough to be certainly crash-orphaned (mirrors
# persist()'s own _STALE_TMP_AGE_SECONDS sweep). A younger temp is
# left; a later delete/persist sweeps it once it ages out.
# nosemgrep: semgrep.rules.path-traversal-risk, reason: Enumerate generated temp names beside the root-checked index base
for stray in parent.glob(f"{base.name}.*.tmp"):
try:
if stray.is_symlink():
cleanup_incomplete = True
logger.warning(
f"Refusing to remove symlinked stray index file: {stray}"
)
continue
if now - stray.stat().st_mtime < _STALE_TMP_AGE_SECONDS:
continue
# nosemgrep: semgrep.rules.path-traversal-risk, reason: Only aged index temp files from the bounded parent glob reach this unlink; symlink leaves are skipped
stray.unlink()
logger.debug(f"Removed stray temp index file: {stray}")
deleted_any = True
except FileNotFoundError:
# Raced a concurrent writer's os.replace() — already gone.
continue
except ValueError:
# A guard refusal must not abort the family delete,
# but it is still reported (the delete returns False).
cleanup_incomplete = True
logger.warning(
f"Could not remove stray index file {stray}"
)
except OSError:
cleanup_incomplete = True
logger.warning(
f"Could not remove stray index file {stray}"
)
# .corrupt-* are quarantine artefacts (and .pkl.corrupt-* is
# PLAINTEXT) — always safe and required to remove, no age gate.
# nosemgrep: semgrep.rules.path-traversal-risk, reason: Enumerate generated quarantine names beside the root-checked index base
for stray in parent.glob(f"{base.name}.corrupt-*"):
try:
if stray.is_symlink():
cleanup_incomplete = True
logger.warning(
f"Refusing to remove symlinked stray index file: {stray}"
)
continue
if stray.is_dir() and stray.name.endswith(".segments"):
delete_segment_files(
Path(str(stray).removesuffix(".segments"))
)
deleted_any = True
continue
# nosemgrep: semgrep.rules.path-traversal-risk, reason: Only files from the bounded quarantine glob reach this unlink; symlink leaves are skipped
stray.unlink()
logger.debug(f"Removed quarantined index file: {stray}")
deleted_any = True
except FileNotFoundError:
continue
except ValueError:
# delete_segment_files refuses foreign/symlinked
# children — one stray must not abort the family
# delete (the plaintext .pkl removal is not optional),
# but the refusal is still reported (the delete
# returns False).
cleanup_incomplete = True
logger.warning(
f"Could not remove stray index file {stray}"
)
except OSError:
cleanup_incomplete = True
logger.warning(
f"Could not remove stray index file {stray}"
)
# A contained refusal (e.g. a foreign segment child) is reported
# as an incomplete delete even though every removable member was
# removed.
return deleted_any and not cleanup_incomplete
except Exception:
logger.exception(f"Failed to delete FAISS files for: {index_path}")
return False
@staticmethod
def _row_provider_key(index: RAGIndex) -> Optional[str]:
"""The provider recorded on ``index`` itself, if the model has one.
``RAGIndex`` has no provider column yet. The first server-backed
provider is expected to add one (the open Milvus PR, #5331, adds
``vector_store_provider``), and once it exists it -- not the current
setting -- is what built the row's vectors: a user who switched
providers can hold rows of both kinds in one collection. Read
defensively so this works before and after that column lands.
"""
raw = getattr(index, "vector_store_provider", None)
if isinstance(raw, str) and raw.strip():
return raw
return None
@staticmethod
def _plan_vector_cleanup(
indices: Iterable[RAGIndex],
settings_snapshot: Optional[dict],
) -> List[_PlannedIndex]:
"""Decide, ONCE per call, how each row's vectors are cleaned up.
A row's provider is its recorded provider when it has one, else the
user's configured provider resolved from ``settings_snapshot``. Doing
this in one pass and handing the result to both the drop and the
delete loop means the two can never disagree (a provider resolved
twice could flip between the reads, skip both the drop and the unlink,
and still delete the rows).
"""
from ....vector_stores import config as vs_config
default_key: Optional[str] = None
store_by_key: Dict[str, Optional[Type[Any]]] = {}
plan: List[_PlannedIndex] = []
for index in indices:
key = CascadeHelper._row_provider_key(index)
if key is None:
if default_key is None:
default_key = vs_config.resolve_provider(settings_snapshot)
key = default_key
if key not in store_by_key:
try:
if vs_config.provider_is_local_file(key, settings_snapshot):
store_by_key[key] = None
else:
store_by_key[key] = vs_config.get_vector_store_class(
key, settings_snapshot
)
except ValueError:
# Unknown/unregistered provider: we cannot tell whether
# there is a remote collection, let alone drop it, so we
# must not delete the row that names it.
raise RemoteVectorStoreDropError(
f"RAGIndex row for collection {index.collection_name} "
"uses an unknown vector-store provider; cannot clean "
"up its vectors.",
retryable=False,
) from None
plan.append(_PlannedIndex(index, key, store_by_key[key]))
return plan
@staticmethod
def _drop_planned_remote_collections(
plan: List[_PlannedIndex],
*,
username: Optional[str],
settings_snapshot: Optional[dict],
) -> int:
"""Drop the remote collections of the server-backed rows in ``plan``.
See :meth:`drop_remote_vector_collections` for the contract.
"""
remote = [
(item, item.remote_store)
for item in plan
if item.remote_store is not None
]
if not remote:
return 0
if not username:
# A server-backed collection's name is derived from the username
# plus index_hash; with no username we cannot name -- hence cannot
# delete -- the collection. Fail loudly rather than delete the rows
# and strand vectors nothing can address.
raise RemoteVectorStoreDropError(
f"Cannot drop {remote[0][0].provider} vector collections without "
"a username; refusing to delete the RAGIndex rows that hold "
"the only handle to them.",
retryable=False,
)
# Validate EVERY row's index_hash before dropping anything. This must
# stay a separate pass ahead of the drop loop below, not a check
# inside it: a bad row sorted after good ones would otherwise let the
# loop drop their collections first and only then abort, destroying
# collections whose rows survive the rollback. Failing here, before
# any drop_collection call, keeps a bad row's abort a true no-op.
for item, _store in remote:
if not item.row.index_hash:
raise RemoteVectorStoreDropError(
f"RAGIndex row for collection {item.row.collection_name} "
f"has no index_hash; cannot address its {item.provider} "
"collection.",
retryable=False,
)
dropped = 0
completed = 0
for item, store_class in remote:
index_hash = item.row.index_hash
try:
if store_class.drop_collection(
username=username,
index_hash=index_hash,
settings_snapshot=settings_snapshot,
):
dropped += 1
except Exception as exc:
# Includes NotImplementedError: a store that declares itself
# server-backed but never implemented a drop path would
# otherwise silently orphan every collection it created.
#
# ``from None``: the cause's text can carry a backend URI or
# token, and a chained cause is printed by any traceback
# logger. Only its type name is kept.
raise RemoteVectorStoreDropError(
f"Failed to drop {item.provider} collection for index "
f"{short_id(index_hash, 12)}: {type(exc).__name__}. The index "
"record was NOT deleted.",
retryable=_is_transient_drop_failure(exc),
dropped=completed,
cause_type=type(exc).__name__,
) from None
completed += 1
logger.info(
f"Dropped {dropped} vector collection(s) "
f"({completed - dropped} already absent) before deleting their "
"RAGIndex records"
)
return dropped
@staticmethod
def drop_remote_vector_collections(
indices: Iterable[RAGIndex],
*,
username: Optional[str],
settings_snapshot: Optional[dict] = None,
) -> int:
"""Drop the server-side collections backing ``indices``.
Rows whose provider is a local-file store are skipped -- those are
cleaned up by unlinking their index files instead -- so this returns 0
for an all-FAISS input. Each row's provider is its recorded provider
when the model has one, else the provider configured in
``settings_snapshot`` (see :meth:`_plan_vector_cleanup`).
MUST be called while the ``RAGIndex`` rows still exist: the backend
collection name is derived from ``(username, index_hash)`` and
``index_hash`` lives on the row. See #5765.
Remote drops are not transactional: if the Nth drop fails, the first
N-1 collections are already gone. The error's ``dropped`` attribute
says how many, so callers can say so instead of "nothing was deleted".
Returns:
The number of collections actually dropped (an already-absent
collection counts as 0 — ``drop_collection`` is idempotent).
Raises:
RemoteVectorStoreDropError: if a server-backed collection could
not be dropped, or cannot be addressed (no ``username``, a row
with no ``index_hash``, an unknown provider). Its
``retryable`` attribute says whether a retry can help. The
caller must abort — and roll back — rather than delete rows.
"""
# Any iterable, a generator included: the plan is a list built in ONE
# pass, and it -- not ``indices`` -- is what the validation pass and
# the drop loop both walk. (Walking a generator twice would exhaust
# it on the first pass and silently skip the validation guard.)
plan = CascadeHelper._plan_vector_cleanup(indices, settings_snapshot)
return CascadeHelper._drop_planned_remote_collections(
plan, username=username, settings_snapshot=settings_snapshot
)
@staticmethod
def delete_rag_indices_for_collection(
session: Session,
collection_name: str,
*,
unlink_files: bool = True,
username: Optional[str] = None,
settings_snapshot: Optional[dict] = None,
) -> Dict[str, Any]:
"""
Delete RAGIndex records (and their vectors) for a collection.
Vector cleanup is provider-aware, decided per row (see
:meth:`_plan_vector_cleanup`):
* Local-file store (FAISS): the ``.faiss``/``.pkl``/``.idmap.json``
files are unlinked — here when ``unlink_files`` is True, otherwise by
the caller after its commit (see ``index_paths`` below).
* Server-backed store: the remote collection is dropped **before** the
``RAGIndex`` rows are deleted, because its name is derived from
``index_hash`` which only exists on those rows. If a drop fails,
:class:`RemoteVectorStoreDropError` propagates before any row is
deleted and the caller must roll back. Collections dropped before
the failing one stay dropped (see the error's ``dropped``) — see
#5765.
The remote drops run inside the caller's transaction (they must
happen while the rows exist, before they are deleted in that same
transaction). If the caller has already written in that transaction,
the user's database write lock is held across those network calls, so
call this before the transaction's first write where possible: the
collection-deletion service does. Collection rebuilds
(``collection_rebuild.py``) run only while every registered provider
stores local files, so their calls make no network call. Only
server-backed stores make network calls here (FAISS never does), and
their ``drop_collection`` is expected to bound each call with a
client timeout.
Args:
session: Database session
collection_name: Name of the collection (e.g., "collection_<uuid>")
unlink_files: When True (default), unlink the FAISS files here.
When False, only the RAGIndex rows are deleted and the index
paths are returned in ``index_paths`` so the caller can unlink
them AFTER its own commit. Unlinking before the commit is
unsafe when more DB work follows before the commit: a later
rollback restores the RAGIndex rows but cannot restore the
already-unlinked files, leaving the collection pointing at a
missing index (silently-broken search). Ignored for
server-backed rows, which have no files.
username: Owner of the indices. Required when a row's provider is
server-backed (it is half of the derived remote collection
name); unused for local-file providers, which is why it stays
optional.
settings_snapshot: The user's settings snapshot. Production
callers MUST pass it: it selects the configured provider for
rows that do not record their own, and carries the connection
config ``drop_collection`` needs. Without it the provider
falls back to the global default (FAISS).
Returns:
Dict with deletion results (``index_paths`` lists local-file rows
only, and only when ``unlink_files`` is False).
Raises:
RemoteVectorStoreDropError: a server-backed collection could not be
dropped or addressed. Deliberately NOT swallowed: completing
the row delete anyway is what makes the leftover vectors
unaddressable.
"""
indices = (
session.query(RAGIndex)
.filter_by(collection_name=collection_name)
.all()
)
# Resolved ONCE; both the drop and the delete loop below read it.
plan = CascadeHelper._plan_vector_cleanup(indices, settings_snapshot)
# ORDERING IS THE FIX (#5765): drop the remote collections FIRST,
# while index_hash is still readable from the rows deleted below. A
# failure raises, so the caller's transaction rolls back with the
# rows — and therefore the vectors' only name — intact.
#
# A partial failure (collection 2 of 3 unreachable) leaves the
# already-dropped collections gone while their rows (and the
# documents' indexed flags) survive the rollback; the error's
# ``dropped`` count lets the caller say so. That direction is
# recoverable — a retried delete or a force re-index reconciles it
# (``drop_collection`` is idempotent) — the opposite order is not.
CascadeHelper._drop_planned_remote_collections(
plan, username=username, settings_snapshot=settings_snapshot
)
deleted_indices = 0
deleted_files = 0
index_paths: List[str] = []
# Only needed on the unlink_files=True path (below) -- computed
# lazily so the unlink_files=False path (every current external
# caller) doesn't pay for it. Same containment root those callers
# pass to their own post-commit delete_faiss_index_files() call
# (see collection_deletion.py, collection_rebuild.py), computed here too so
# unlink_files=True gets the same symlinked-ancestor defense --
# currently unreachable (all callers pass unlink_files=False) but
# kept in sync to avoid a latent gap if that changes.
#
# NOTE: this is the COARSE shared rag_indices/ root, not the tighter
# per-user rag_indices/<sha256(user)>/ subdir. It is intentionally
# NOT narrowed: 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 refuse to delete a legacy-layout index and
# orphan its files. Kept consistent with the external callers above.
rag_indices_root = None
if unlink_files and any(item.remote_store is None for item in plan):
from ....config.paths import get_cache_directory
rag_indices_root = get_cache_directory() / "rag_indices"
for item in plan:
index = item.row
# index_path is only meaningful for a local-file store. For a
# server-backed row it names no file, so neither unlinking it here
# nor handing it to the caller's post-commit unlink would do
# anything but log a confusing miss — its vectors were already
# dropped above.
if item.remote_store is None:
path = str(index.index_path)
if unlink_files:
if CascadeHelper.delete_faiss_index_files(
path, allowed_root=rag_indices_root
):
deleted_files += 1
else:
index_paths.append(path)
session.delete(index)
deleted_indices += 1
logger.debug(
f"Deleted {deleted_indices} RAGIndex records and {deleted_files} "
f"index files for collection {collection_name}"
)
return {
"deleted_indices": deleted_indices,
"deleted_files": deleted_files,
"index_paths": index_paths,
}
@staticmethod
def update_download_tracker(
session: Session,
document: Document,
) -> bool:
"""
Update DownloadTracker when a document is deleted.
The FK has SET NULL, but we also need to update is_downloaded flag.
Args:
session: Database session
document: The document being deleted
Returns:
True if tracker was updated
"""
if not document.original_url:
return False
# URL-equivalent trackers can belong to different PDF versions.
# Reset only a tracker whose content hash or document link matches,
# retaining the legacy URL-hash fallback for the deleted document.
from ...utils import find_tracker_for_document
try:
tracker = find_tracker_for_document(session, document)
if tracker:
tracker.is_downloaded = False # type: ignore[assignment]
tracker.file_path = None # type: ignore[assignment]
logger.debug(
f"Updated DownloadTracker for document {short_id(document.id)}"
)
return True
except Exception:
logger.exception("Failed to update DownloadTracker")
return False
@staticmethod
def count_document_in_collections(
session: Session,
document_id: str,
) -> int:
"""
Count how many collections a document is in.
Args:
session: Database session
document_id: The document ID
Returns:
Number of collections the document is in
"""
return (
session.query(DocumentCollection)
.filter_by(document_id=document_id)
.count()
)
@staticmethod
def get_document_collections(
session: Session,
document_id: str,
) -> List[str]:
"""
Get all collection IDs a document belongs to.
Args:
session: Database session
document_id: The document ID
Returns:
List of collection IDs
"""
doc_collections = (
session.query(DocumentCollection.collection_id)
.filter_by(document_id=document_id)
.all()
)
return [dc.collection_id for dc in doc_collections]
@staticmethod
def delete_document_completely(
session: Session,
document_id: str,
) -> bool:
"""
Delete a document and all related records using query-based deletes.
This avoids ORM cascade issues where SQLAlchemy tries to set
DocumentBlob.document_id to NULL (which fails because it's a PK).
Deletes in order:
1. DocumentBlob
2. DocumentCollection links
3. Document itself
Note: DocumentChunks should be deleted separately before calling this,
as they may need collection-specific handling.
Args:
session: Database session
document_id: The document ID to delete
Returns:
True if document was deleted
"""
# Delete blob (has document_id as PK, can't be nulled by cascade)
session.query(DocumentBlob).filter_by(document_id=document_id).delete(
synchronize_session=False
)
# Delete collection links
session.query(DocumentCollection).filter_by(
document_id=document_id
).delete(synchronize_session=False)
# Delete document itself
deleted = (
session.query(Document)
.filter_by(id=document_id)
.delete(synchronize_session=False)
)
if deleted:
logger.debug(f"Deleted document {short_id(document_id)} completely")
return deleted > 0