# 4. Delete extraction

<mark style="color:green;">`DELETE`</mark> `/deleteExtraction`

This endpoint enables the deletion of an entire extraction process, a specific batch within an extraction, or an individual file, depending on the parameters provided in the request body. The action is permanent and cannot be undone.

* Providing only the `extractionId` results in the deletion of the entire **extraction** process along with all associated **batches** and **files**.
* Specifying both `extractionId` and `batchId` deletes the specified **batch** and **all files** within it from the extraction.
* Including `extractionId`, `batchId`, and `fileId` leads to the deletion of a **specific** **file** within a batch.

## Postman Collection

For a complete and interactive set of API requests, please refer to our [Postman Integration](/data-extraction-api/postman-integration.md)collection.

## Server URL

```
https://api.extracta.ai/api/v1
```

## Headers

| Name          | Value              |
| ------------- | ------------------ |
| Content-Type  | `application/json` |
| Authorization | `Bearer <token>`   |

## Body

<table><thead><tr><th width="183">Name</th><th width="135">Type</th><th width="164">Required</th><th>Description</th></tr></thead><tbody><tr><td><code>extractionId</code></td><td>string</td><td><code>true</code></td><td>The extraction id</td></tr><tr><td><code>batchId</code></td><td>string</td><td><code>false</code></td><td>The batch id</td></tr><tr><td><code>fileId</code></td><td>string</td><td><code>false</code></td><td>The file id</td></tr></tbody></table>

## Body Example

```json
{
    "extractionId": "extractionId",
    "batchId": "batchId",
    "fileId": "fileId"
}
```

## Code Example

{% tabs %}
{% tab title="JavaScript" %}

```javascript
const axios = require('axios');

/**
 * Deletes a specific file within a batch of an extraction process.
 * 
 * @param {string} token - The authorization token for API access.
 * @param {Object} deletionDetails - The identifiers for the extraction, batch, and file to be deleted.
 * @returns {Promise<Object>} The promise that resolves to the API response confirming deletion.
 */
async function deleteExtraction(token, deletionDetails) {
    const url = "https://api.extracta.ai/api/v1/deleteExtraction";

    try {
        const response = await axios.delete(url, deletionDetails, {
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${token}`
            }
        });

        // Handling response
        return response.data; // Directly return the parsed JSON response
    } catch (error) {
        // Handling errors
        throw error.response ? error.response.data : new Error('An unknown error occurred');
    }
}

async function main() {
    const token = 'apiKey';
    const deletionDetails = {
        "extractionId": "yourExtractionId",
        "batchId": "yourBatchId",
        "fileId": "yourFileId"
    };

    try {
        const response = await deleteExtraction(token, deletionDetails);
        console.log(response);
    } catch (error) {
        console.error(error);
    }
}

main();
```

{% endtab %}

{% tab title="Python" %}

```python
import requests

def delete_extraction(token, deletion_details):
    url = "https://api.extracta.ai/api/v1/deleteExtraction"
    headers = {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}

    try:
        response = requests.delete(url, json=deletion_details, headers=headers)
        response.raise_for_status()  # Raises an HTTPError if the response status code indicates an error
        return response.json()  # Returns the parsed JSON response
    except requests.RequestException as e:
        # Handles any requests-related errors
        print(e)
        return None

# Example usage
if __name__ == "__main__":
    token = "apiKey"
    deletion_details = {
        "extractionId": "yourExtractionId",
        "batchId": "yourBatchId",
        "fileId": "yourFileId"
    }

    response = delete_extraction(token, deletion_details)
    if response:
        print(response)
    else:
        print("Failed to delete.")
```

{% endtab %}

{% tab title="PHP" %}

```php
<?php

/**
 * Deletes an extraction, a specific batch within an extraction, or a specific file within a batch,
 * depending on the parameters provided.
 * 
 * @param string $token The authorization token for API access.
 * @param array $deletionDetails The identifiers for the extraction, batch, and file to be deleted.
 * @return mixed The API response confirming the deletion or an error message.
 */
function deleteExtraction($token, $deletionDetails) {
    $url = 'https://api.extracta.ai/api/v1/deleteExtraction';

    // Initialize cURL session
    $ch = curl_init($url);

    // Prepare the payload
    $payload = json_encode($deletionDetails);

    // Set cURL options
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $token,
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    try {
        // Execute cURL session
        $response = curl_exec($ch);

        // Check for cURL errors
        if (curl_errno($ch)) {
            throw new Exception('Curl error: ' . curl_error($ch));
        }

        // Optionally, you might want to check the response status code here

        // For simplicity, returning the decoded response for now
        return $response;
    } catch (Exception $e) {
        // Handle exceptions or errors here
        return 'Error: ' . $e->getMessage();
    } finally {
        // Always close the cURL session
        curl_close($ch);
    }
}

// Example usage
$token = 'apiKey';
$deletionDetails = [
    "extractionId" => "yourExtractionId",
    "batchId" => "yourBatchId", // Optional, depends on the use case
    "fileId" => "yourFileId" // Optional, depends on the use case
];

try {
    $response = deleteExtraction($token, $deletionDetails);
    echo "Deletion Response: " . $response;
} catch (Exception $e) {
    echo "Failed to delete: " . $e->getMessage();
}

?>
```

{% endtab %}
{% endtabs %}

## Responses

{% tabs %}
{% tab title="200" %}

```json
{
    "status": "deleted",
    "deletedAt": 1712547789609
}
```

{% endtab %}

{% tab title="400" %}

```json
{
    "status": "error",
    "message": "Extraction id is required"
}
```

{% endtab %}

{% tab title="500" %}

```json
{
    "status": "error",
    "message": "Could not delete extraction"
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.extracta.ai/data-extraction-api/api-endpoints-data-extraction/4.-delete-extraction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
