删除文档文件
1 min read
delete/knowledge-fs/spaces/{knowledge_base_id}/documents/{document_file_id}
Request example
curl --request DELETE \
--url https://api.dify.ai/v1/knowledge-fs/spaces/{knowledge_base_id}/documents/{document_file_id} \
--header 'Authorization: Bearer <token>' \
--header 'Idempotency-Key: <string>' \
--header 'Content-Type: application/json' \
--data '{
"expectedRevision": 1
}'import requests
url = "https://api.dify.ai/v1/knowledge-fs/spaces/{knowledge_base_id}/documents/{document_file_id}"
headers = {
"Authorization": "Bearer <token>",
"Idempotency-Key": "<string>",
"Content-Type": "application/json",
}
payload = {
"expectedRevision": 1
}
response = requests.request("DELETE", url, headers=headers, json=payload)
print(response.text)const response = await fetch('https://api.dify.ai/v1/knowledge-fs/spaces/{knowledge_base_id}/documents/{document_file_id}', {
method: 'DELETE',
headers: {
'Authorization': 'Bearer <token>',
'Idempotency-Key': '<string>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"expectedRevision": 1
})
});
const data = await response.json();
console.log(data);<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.dify.ai/v1/knowledge-fs/spaces/{knowledge_base_id}/documents/{document_file_id}',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_POSTFIELDS => '{
"expectedRevision": 1
}',
CURLOPT_HTTPHEADER => [
'Authorization: Bearer <token>',
'Idempotency-Key: <string>',
'Content-Type: application/json',
],
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;package main
import (
"fmt"
"io"
"net/http"
"strings"
)
func main() {
url := "https://api.dify.ai/v1/knowledge-fs/spaces/{knowledge_base_id}/documents/{document_file_id}"
payload := strings.NewReader("{\n \"expectedRevision\": 1\n}")
req, _ := http.NewRequest("DELETE", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Idempotency-Key", "<string>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.dify.ai/v1/knowledge-fs/spaces/{knowledge_base_id}/documents/{document_file_id}"))
.header("Authorization", "Bearer <token>")
.header("Idempotency-Key", "<string>")
.header("Content-Type", "application/json")
.method("DELETE", HttpRequest.BodyPublishers.ofString("{\n \"expectedRevision\": 1\n}"))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());require 'uri'
require 'net/http'
url = URI('https://api.dify.ai/v1/knowledge-fs/spaces/{knowledge_base_id}/documents/{document_file_id}')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = url.scheme == 'https'
request = Net::HTTP::Delete.new(url)
request['Authorization'] = 'Bearer <token>'
request['Idempotency-Key'] = '<string>'
request['Content-Type'] = 'application/json'
request.body = '{
"expectedRevision": 1
}'
response = http.request(request)
puts response.read_bodyResponse
{
"document_title": null,
"job": {
"checkpoint": "requested",
"completed_at": null,
"created_at": "2026-09-06T02:25:00Z",
"error": null,
"id": "b7e1c2d3-4f5a-4b6c-8d9e-0f1a2b3c4d5e",
"knowledge_space_id": "9b2e5b7a-3c1d-4e2f-8a9b-1c2d3e4f5a6b",
"mode": "cascade",
"progress": null,
"retry_at": null,
"run_state": "dispatch_pending",
"target_id": "6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b",
"target_type": "document",
"updated_at": "2026-09-06T02:25:00Z"
},
"status_url": "https://api.example.com/v1/knowledge-fs/spaces/8d2c6a1e-5b3f-4c7d-9e01-2a3b4c5d6e7f/deletion-jobs/b7e1c2d3-4f5a-4b6c-8d9e-0f1a2b3c4d5e"
}{
"code": "knowledge_fs_invalid_request",
"message": "Agent Knowledge Base request is invalid.",
"status": 400,
"violations": [
{
"field": [
"expectedRevision"
],
"type": "missing"
}
]
}{
"code": "knowledge_fs_invalid_request",
"message": "Agent Knowledge Base request is invalid.",
"status": 400
}{
"code": "knowledge_fs_invalid_request",
"failure": {
"category": "validation",
"code": "KNOWLEDGE_FS_INVALID_REQUEST",
"message": "The Agent Knowledge Base request is invalid.",
"retryPolicy": "never",
"traceId": "7f3c1a90-2e64-4b7d-8a51-6c0d9e2f4b83"
},
"message": "The Agent Knowledge Base request is invalid.",
"status": 400
}{
"code": "knowledge_fs_invalid_credential",
"message": "Invalid Agent Knowledge Base service credential.",
"status": 401
}{
"code": "unauthorized",
"message": "Access token is invalid",
"status": 401
}{
"code": "forbidden",
"message": "Sorry, you have reached the knowledge base request rate limit of your subscription.",
"status": 403
}{
"code": "knowledge_fs_access_denied",
"message": "Agent Knowledge Base operation is not allowed.",
"status": 403
}{
"code": "knowledge_fs_access_denied",
"failure": {
"action": "contact_admin",
"category": "authorization",
"code": "KNOWLEDGE_FS_ACCESS_DENIED",
"message": "You do not have permission to perform this Agent Knowledge Base operation.",
"retryPolicy": "never",
"traceId": "7f3c1a90-2e64-4b7d-8a51-6c0d9e2f4b83"
},
"message": "You do not have permission to perform this Agent Knowledge Base operation.",
"status": 403
}{
"code": "knowledge_fs_resource_not_found",
"failure": {
"category": "not_found",
"code": "KNOWLEDGE_FS_NOT_FOUND",
"message": "The requested Agent Knowledge Base resource was not found.",
"retryPolicy": "never",
"traceId": "7f3c1a90-2e64-4b7d-8a51-6c0d9e2f4b83"
},
"message": "The requested Agent Knowledge Base resource was not found.",
"status": 404
}{
"code": "not_found",
"message": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.",
"status": 404
}{
"code": "knowledge_fs_conflict",
"failure": {
"category": "conflict",
"code": "DURABLE_DELETION_IDEMPOTENCY_CONFLICT",
"message": "The Agent Knowledge Base operation conflicts with the current resource state.",
"retryPolicy": "never",
"traceId": "5e2a9c17-3b4d-4f60-8a1b-2c3d4e5f6a7b"
},
"message": "The Agent Knowledge Base operation conflicts with the current resource state.",
"status": 409
}{
"code": "knowledge_fs_conflict",
"failure": {
"action": "retry",
"category": "conflict",
"code": "KNOWLEDGE_FS_CONFLICT",
"message": "The Agent Knowledge Base operation conflicts with the current resource state.",
"retryPolicy": "manual",
"traceId": "7f3c1a90-2e64-4b7d-8a51-6c0d9e2f4b83"
},
"message": "The Agent Knowledge Base operation conflicts with the current resource state.",
"status": 409
}{
"code": "knowledge_fs_upstream_unavailable",
"failure": {
"action": "contact_admin",
"category": "internal",
"code": "KNOWLEDGE_FS_INTERNAL_ERROR",
"message": "Agent Knowledge Base could not complete the operation. Try again, or contact an administrator with the error reference.",
"retryPolicy": "manual",
"traceId": "5e2a9c17-3b4d-4f60-8a1b-2c3d4e5f6a7b"
},
"message": "Agent Knowledge Base could not complete the operation. Try again, or contact an administrator with the error reference.",
"status": 502
}{
"code": "knowledge_fs_upstream_unavailable",
"message": "Agent Knowledge Base is unavailable.",
"status": 502
}{
"code": "knowledge_fs_operation_unavailable",
"message": "Agent Knowledge Base operation is not available.",
"status": 503
}{
"code": "knowledge_fs_operation_unavailable",
"failure": {
"action": "retry",
"category": "dependency",
"code": "KNOWLEDGE_FS_UNAVAILABLE",
"message": "A service required by Agent Knowledge Base is temporarily unavailable.",
"retryPolicy": "manual",
"traceId": "5e2a9c17-3b4d-4f60-8a1b-2c3d4e5f6a7b"
},
"message": "A service required by Agent Knowledge Base is temporarily unavailable.",
"status": 503
}delete/knowledge-fs/spaces/{knowledge_base_id}/documents/{document_file_id}
Authorization
Path parameters
知识库 ID:即知识库 API 访问 对话框中所示服务 API 端点的最后一段(
…/knowledge-fs/spaces/<knowledge base ID>),也是地址栏中 /datasets/new/ 之后的那一段。Headers
用同一个 API 密钥重复发送相同的键和请求,会以
202 返回原作业。将该键用于其他请求,或通过其他 API 密钥使用,会返回 409。使用可打印 ASCII 字符,首尾不得有空白。Length: 8–255
Body
application/json文档文件当前的
version,从 获取文档文件列表 或 获取文档文件 获取。文档文件的 version 始终为 1。不是 获取文档修订版本列表 返回的修订版本号。值已过时则返回 409,不会删除任何内容。Range: >= 1
Responses
application/json
删除请求已受理,在后台进行。从删除请求被受理起,获取文档文件 即返回
404。如需确认删除已结束,轮询 获取删除作业(即 status_url),直到 run_state 为 completed 或 failed。Show child attributesHide child attributes
删除进行到哪一步:从
requested(刚受理)开始,依次经过 quiescing、deleting_objects、deleting_derived_data、deleting_primary_data,直到 completed。作业完成时间,ISO 8601 格式。完成前以及作业为
failed 时为 null。作业的受理时间,ISO 8601 格式。
最近一次错误,没有错误时为
null。run_state 为 retry_wait 时也会设置此字段,因此判断作业是否失败应看 run_state,而不是 error。删除作业 ID。
知识库的内部 ID,并非路径中的知识库 ID。
删除数据源时如何处理其文档:
cascade 一并删除,keep 保留。删除文档文件或文档时,忽略该值。多项删除的项数统计。此 API 不填充该字段,始终为
null。下次自动重试的时间,ISO 8601 格式。未安排重试时为
null。调度状态:
dispatch_pending(刚受理)、queued、running:进行中。retry_wait:已安排在retry_at自动重试。completed:已全部移除。failed:清理已停止,删除目标仍保持隐藏,且无法重试。
删除目标的 ID:
document 时为文档文件 ID,logical_document 时为文档 ID,source 时为数据源 ID。作业删除的对象:
document 表示文档文件,logical_document 表示整个文档,source 表示数据源。作业最后变更时间,ISO 8601 格式。