Delete a document

Updated at:

This topic describes the syntax for deleting a document in Tongyi Data Mining and provides examples.

Request syntax

POST /zhiwen-file/delete_file HTTP/1.1

Request parameters

Name

Type

Required

Description

Example

fileId

string

Yes

The ID of the document to delete.

file_zhiwen_XXX

Response parameters

Name

Type

Description

code

int

The status code.

request_id

string

The request ID.

success

boolean

Indicates whether the request was successful.

message

string

The response message.

Examples

Request examples

import os
import requests
from http import HTTPStatus

'''
API name: Delete a document
API path: https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/delete_file
Environment requirements: Python >= 3.7
'''

url = "https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/delete_file"
request_headers = {
    "Authorization": os.environ.get("DASHSCOPE_API_KEY"),  # If you have not configured the environment variable, replace the variable with your API key.
    "Content-Type": "application/json"
}

response = requests.post(url,
                         json={
                             "fileId": "file_zhiwen_5f6e46d308a145b182c4bf1b788cea07_10035945"
                         },
                         headers=request_headers)

if response.status_code == HTTPStatus.OK:
    print(response.json())
else:
    print(f'response={response.json()}')
    print(f'code={response.status_code}')
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * API name: Delete a document
 * API path: https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/delete_file
 * Environment requirements: Java 8 or later
 */
public class FileDeleteDemo {
    // Step 1. Configure the API endpoint and other settings.
    private static final String API_KEY = System.getenv("DASHSCOPE_API_KEY");    // Specify the API key for Model Studio.
    private static final String API_URL = "https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/delete_file";
    private static final String CHARSET = "UTF-8";
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) throws IOException {
        // Step 2. Configure the request parameters.
        // The fileId field returned by the document upload API. The format is file_zhiwen_XXX.
        String fileId = "file_zhiwen_XXX";

        // Step 3. Build the request body.
        Map<String, String> requestBodyMap = new HashMap<>();
        requestBodyMap.put("fileId", fileId);
        String requestBody = objectMapper.writeValueAsString(requestBodyMap);

        // Step 4. Configure the request timeout period.
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000).build();

        // Step 5. Send the request to delete the document.
        try (CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .build()) {
            // Construct an HTTP POST request.
            HttpPost httpPost = new HttpPost(API_URL);
            buildRequestHeaders(httpPost, requestBody);
            HttpResponse response = httpClient.execute(httpPost);

            // Check the response status code.
            if (response.getStatusLine().getStatusCode() == 200) {
                System.out.println("Document deleted successfully: " + fileId);
            } else {
                System.out.println("Failed to delete the document: " + fileId);
            }
        }catch (Exception e) {
            System.err.println("An exception occurred while deleting the document: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * Build common request headers.
     *
     * @param httpPost The HTTP request object.
     * @param requestBody The request body string.
     */
    private static void buildRequestHeaders(HttpPost httpPost, String requestBody) {
        httpPost.setHeader("Authorization", API_KEY);
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(requestBody, CHARSET));
    }
}

Successful response example

JSON format

{
  "code": 200,
  "request_id": "1a022107-2b41-979f-a730-59b43d3ada2c",
  "success": true,
  "message": "Success"
}

Error codes

For more information, see Error codes for Tongyi Data Mining.