langgenius/dify · warning · DocumentIndexingError

document_indexing

document_indexing

Error message

Cannot delete document during indexing.

What it means

DocumentIndexingError (HTTP 400, error_code=document_indexing) raised when DocumentService.delete_documents throws the service-layer DocumentIndexingError — at least one targeted document is in an active indexing state. The controller re-raises with the 'Cannot delete document during indexing.' message to protect vector-store consistency.

Source

Thrown at api/controllers/console/datasets/datasets_document.py:615

        if dataset is None:
            raise NotFound("Dataset not found.")

        if not current_user.is_dataset_editor:
            raise Forbidden()

        if not dify_config.RBAC_ENABLED:
            try:
                DatasetService.check_dataset_permission(dataset, current_user, session)
            except services.errors.account.NoPermissionError as e:
                raise Forbidden(str(e))

        check_knowledge_rate_limit()
        try:
            document_ids = request.args.getlist("document_id")
            dataset_ref = DatasetRefService.create_dataset_ref(dataset)
            DocumentService.delete_documents(dataset_ref, document_ids, dataset.get_doc_form(session=session), session)
        except services.errors.document.DocumentIndexingError:
            raise DocumentIndexingError("Cannot delete document during indexing.")

        return "", 204


@console_ns.route("/datasets/init")
class DatasetInitApi(Resource):
    @console_ns.doc("init_dataset")
    @console_ns.doc(description="Initialize dataset with documents")
    @console_ns.expect(console_ns.models[KnowledgeConfig.__name__])
    @console_ns.response(
        200, "Dataset initialized successfully", console_ns.models[DatasetAndDocumentResponse.__name__]
    )
    @console_ns.response(400, "Invalid request parameters")
    @setup_required
    @login_required
    @account_initialization_required
    @cloud_edition_billing_resource_check("vector_space")
    @cloud_edition_billing_rate_limit_check("knowledge")

View on GitHub (pinned to ef8544b173)

Solutions

  1. Poll GET /console/api/datasets/{dataset_id}/documents/{document_id}/indexing-status until it reaches a terminal state, then retry the delete.
  2. Cancel the in-flight indexing task via the documents API before deleting.
  3. Filter the document_id list to only those not currently indexing before re-submitting.
  4. If indexing is stuck, investigate the celery worker / indexing queue rather than force-deleting.
Defensive patterns

Strategy: retry

Validate before calling

import requests, time

def documents_deletable(base_url, headers, dataset_id, doc_ids):
    r = requests.get(f"{base_url}/console/api/datasets/{dataset_id}/documents",
                     headers=headers)
    r.raise_for_status()
    inflight = {'waiting', 'parsing', 'indexing', 'cleaning', 'splitting'}
    by_id = {d['id']: d for d in r.json().get('data', [])}
    return all(by_id[i].get('indexing_status') not in inflight for i in doc_ids if i in by_id)

# only delete when nothing is in-flight
if not documents_deletable(base, hdrs, dataset_id, doc_ids):
    raise SystemExit('some documents still indexing; wait and retry')

Type guard

def is_terminal_status(status: str) -> bool:
    return status in {'completed', 'error'}

Try / catch

for attempt in range(6):
    resp = requests.delete(f"{base}/console/api/datasets/{dataset_id}/documents",
                           headers=hdrs, params={'document_id': doc_ids})
    if resp.status_code == 400 and resp.json().get('code') == 'document_indexing':
        time.sleep(2 ** attempt)  # backoff, then re-check status
        continue
    resp.raise_for_status()
    break
else:
    raise RuntimeError('documents still indexing after retries')

Prevention

When it happens

Trigger: DELETE /console/api/datasets/{dataset_id}/documents?document_id=... while any listed document has indexing_status in an in-flight state (waiting/parsing/indexing/queue).

Common situations: User clicks delete immediately after upload before indexing settles; retry of a delete that raced with a re-index; long-running indexing job on a large file.

Related errors


AI-assisted analysis of langgenius/dify@ef8544b173 (2026-08-12). Data as JSON: /api/errors/fa3e56204a9bd27e. Report an issue: GitHub.