{"record":{"id":"fa3e56204a9bd27e","repo":"langgenius/dify","slug":"document-indexing","errorCode":"document_indexing","errorMessage":"Cannot delete document during indexing.","messagePattern":"Cannot delete document during indexing\\.","errorType":"error_code","errorClass":"DocumentIndexingError","httpStatus":400,"severity":"warning","filePath":"api/controllers/console/datasets/datasets_document.py","lineNumber":615,"sourceCode":"        if dataset is None:\n            raise NotFound(\"Dataset not found.\")\n\n        if not current_user.is_dataset_editor:\n            raise Forbidden()\n\n        if not dify_config.RBAC_ENABLED:\n            try:\n                DatasetService.check_dataset_permission(dataset, current_user, session)\n            except services.errors.account.NoPermissionError as e:\n                raise Forbidden(str(e))\n\n        check_knowledge_rate_limit()\n        try:\n            document_ids = request.args.getlist(\"document_id\")\n            dataset_ref = DatasetRefService.create_dataset_ref(dataset)\n            DocumentService.delete_documents(dataset_ref, document_ids, dataset.get_doc_form(session=session), session)\n        except services.errors.document.DocumentIndexingError:\n            raise DocumentIndexingError(\"Cannot delete document during indexing.\")\n\n        return \"\", 204\n\n\n@console_ns.route(\"/datasets/init\")\nclass DatasetInitApi(Resource):\n    @console_ns.doc(\"init_dataset\")\n    @console_ns.doc(description=\"Initialize dataset with documents\")\n    @console_ns.expect(console_ns.models[KnowledgeConfig.__name__])\n    @console_ns.response(\n        200, \"Dataset initialized successfully\", console_ns.models[DatasetAndDocumentResponse.__name__]\n    )\n    @console_ns.response(400, \"Invalid request parameters\")\n    @setup_required\n    @login_required\n    @account_initialization_required\n    @cloud_edition_billing_resource_check(\"vector_space\")\n    @cloud_edition_billing_rate_limit_check(\"knowledge\")","sourceCodeStart":597,"sourceCodeEnd":633,"githubUrl":"https://github.com/langgenius/dify/blob/ef8544b173fd6cd7a8e71df2cab576e52bebbfbc/api/controllers/console/datasets/datasets_document.py#L597-L633","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Poll GET /console/api/datasets/{dataset_id}/documents/{document_id}/indexing-status until it reaches a terminal state, then retry the delete.","Cancel the in-flight indexing task via the documents API before deleting.","Filter the document_id list to only those not currently indexing before re-submitting.","If indexing is stuck, investigate the celery worker / indexing queue rather than force-deleting."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"import requests, time\n\ndef documents_deletable(base_url, headers, dataset_id, doc_ids):\n    r = requests.get(f\"{base_url}/console/api/datasets/{dataset_id}/documents\",\n                     headers=headers)\n    r.raise_for_status()\n    inflight = {'waiting', 'parsing', 'indexing', 'cleaning', 'splitting'}\n    by_id = {d['id']: d for d in r.json().get('data', [])}\n    return all(by_id[i].get('indexing_status') not in inflight for i in doc_ids if i in by_id)\n\n# only delete when nothing is in-flight\nif not documents_deletable(base, hdrs, dataset_id, doc_ids):\n    raise SystemExit('some documents still indexing; wait and retry')","typeGuard":"def is_terminal_status(status: str) -> bool:\n    return status in {'completed', 'error'}","tryCatchPattern":"for attempt in range(6):\n    resp = requests.delete(f\"{base}/console/api/datasets/{dataset_id}/documents\",\n                           headers=hdrs, params={'document_id': doc_ids})\n    if resp.status_code == 400 and resp.json().get('code') == 'document_indexing':\n        time.sleep(2 ** attempt)  # backoff, then re-check status\n        continue\n    resp.raise_for_status()\n    break\nelse:\n    raise RuntimeError('documents still indexing after retries')","preventionTips":["Disable the delete button in the UI while indexing_status is in-flight.","Poll the indexing-status endpoint with backoff before retrying.","Cancel the indexing task explicitly if you must delete now.","Filter the document_id list to terminal-status documents before submitting."],"tags":["datasets","indexing","concurrency","delete","state"],"backgroundTag":null,"analyzedSha":"ef8544b173fd6cd7a8e71df2cab576e52bebbfbc","analyzedAt":"2026-08-12T05:15:17.394Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}