Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

feat: add dataset delete endpoint (#5048)

Pascal M415816cf357c
diff --git a/api/controllers/service_api/dataset/dataset.py b/api/controllers/service_api/dataset/dataset.py
index bf08291d7b9..dcbfa88d994 100644
--- a/api/controllers/service_api/dataset/dataset.py
+++ b/api/controllers/service_api/dataset/dataset.py
@@ -1,5 +1,6 @@
from flask import request
from flask_restful import marshal, reqparse
+from werkzeug.exceptions import NotFound
import services.dataset_service
from controllers.service_api import api
@@ -19,10 +20,12 @@ def _validate_name(name):
return name
-class DatasetApi(DatasetApiResource):
- """Resource for get datasets."""
+class DatasetListApi(DatasetApiResource):
+ """Resource for datasets."""
def get(self, tenant_id):
+ """Resource for getting datasets."""
+
page = request.args.get('page', default=1, type=int)
limit = request.args.get('limit', default=20, type=int)
provider = request.args.get('provider', default="vendor")
@@ -65,9 +68,9 @@ class DatasetApi(DatasetApiResource):
}
return response, 200
- """Resource for datasets."""
def post(self, tenant_id):
+ """Resource for creating datasets."""
parser = reqparse.RequestParser()
parser.add_argument('name', nullable=False, required=True,
help='type is required. Name must be between 1 to 40 characters.',
@@ -89,6 +92,31 @@ class DatasetApi(DatasetApiResource):
return marshal(dataset, dataset_detail_fields), 200
+class DatasetApi(DatasetApiResource):
+ """Resource for dataset."""
+
+ def delete(self, _, dataset_id):
+ """
+ Deletes a dataset given its ID.
+
+ Args:
+ dataset_id (UUID): The ID of the dataset to be deleted.
+
+ Returns:
+ dict: A dictionary with a key 'result' and a value 'success'
+ if the dataset was successfully deleted. Omitted in HTTP response.
+ int: HTTP status code 204 indicating that the operation was successful.
+
+ Raises:
+ NotFound: If the dataset with the given ID does not exist.
+ """
+
+ dataset_id_str = str(dataset_id)
-api.add_resource(DatasetApi, '/datasets')
+ if DatasetService.delete_dataset(dataset_id_str, current_user):
+ return {'result': 'success'}, 204
+ else:
+ raise NotFound("Dataset not found.")
+api.add_resource(DatasetListApi, '/datasets')
+api.add_resource(DatasetApi, '/datasets/<uuid:dataset_id>')
diff --git a/web/app/(commonLayout)/datasets/template/template.en.mdx b/web/app/(commonLayout)/datasets/template/template.en.mdx
index 3775d30d246..36395d391de 100644
--- a/web/app/(commonLayout)/datasets/template/template.en.mdx
+++ b/web/app/(commonLayout)/datasets/template/template.en.mdx
@@ -345,6 +345,43 @@ import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from
---
+<Heading
+ url='/datasets/{dataset_id}'
+ method='DELETE'
+ title='Delete knowledge'
+ name='#delete_dataset'
+/>
+<Row>
+ <Col>
+ ### Params
+ <Properties>
+ <Property name='dataset_id' type='string' key='dataset_id'>
+ Knowledge ID
+ </Property>
+ </Properties>
+ </Col>
+ <Col sticky>
+ <CodeGroup
+ title="Request"
+ tag="DELETE"
+ label="/datasets/{dataset_id}"
+ targetCode={`curl --location --request DELETE '${props.apiBaseUrl}/datasets/{dataset_id}' \\\n--header 'Authorization: Bearer {api_key}'`}
+ >
+ ```bash {{ title: 'cURL' }}
+ curl --location --request DELETE '${props.apiBaseUrl}/datasets/{dataset_id}' \
+ --header 'Authorization: Bearer {api_key}'
+ ```
+ </CodeGroup>
+ <CodeGroup title="Response">
+ ```text {{ title: 'Response' }}
+ 204 No Content
+ ```
+ </CodeGroup>
+ </Col>
+</Row>
+
+---
+
<Heading
url='/datasets/{dataset_id}/documents/{document_id}/update_by_text'
method='POST'
diff --git a/web/app/(commonLayout)/datasets/template/template.zh.mdx b/web/app/(commonLayout)/datasets/template/template.zh.mdx
index 1806969497b..a624c0594fe 100644
--- a/web/app/(commonLayout)/datasets/template/template.zh.mdx
+++ b/web/app/(commonLayout)/datasets/template/template.zh.mdx
@@ -345,6 +345,43 @@ import { Row, Col, Properties, Property, Heading, SubProperty, Paragraph } from
---
+<Heading
+ url='/datasets/{dataset_id}'
+ method='DELETE'
+ title='删除知识库'
+ name='#delete_dataset'
+/>
+<Row>
+ <Col>
+ ### Path
+ <Properties>
+ <Property name='dataset_id' type='string' key='dataset_id'>
+ 知识库 ID
+ </Property>
+ </Properties>
+ </Col>
+ <Col sticky>
+ <CodeGroup
+ title="Request"
+ tag="DELETE"
+ label="/datasets/{dataset_id}"
+ targetCode={`curl --location --request DELETE '${props.apiBaseUrl}/datasets/{dataset_id}' \\\n--header 'Authorization: Bearer {api_key}'`}
+ >
+ ```bash {{ title: 'cURL' }}
+ curl --location --request DELETE '${props.apiBaseUrl}/datasets/{dataset_id}' \
+ --header 'Authorization: Bearer {api_key}'
+ ```
+ </CodeGroup>
+ <CodeGroup title="Response">
+ ```text {{ title: 'Response' }}
+ 204 No Content
+ ```
+ </CodeGroup>
+ </Col>
+</Row>
+
+---
+
<Heading
url='/datasets/{dataset_id}/documents/{document_id}/update_by_text'
method='POST'