How to Delete Files On-Demand with Python

Learn how to immediately delete uploaded or generated files with the pdfRest Delete Files API using Python.
Share this page

Why Delete Files with Python?

The pdfRest Delete Files API Tool enables users to efficiently remove files from the pdfRest server using a straightforward API call. This tutorial will guide you through the process of sending an API call to the Delete Files endpoint using Python, showcasing how simple it is to integrate this functionality into your applications.

A user may need to delete files from the server to comply with data compliance policies. For instance, a business might regularly upload and process sensitive documents, requiring them to delete these files after processing to ensure data privacy and security.

Delete Files with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

# By default, we use the US-based API service. This is the primary endpoint for global use.
api_url = "https://api.pdfrest.com"

# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
#api_url = "https://eu-api.pdfrest.com"

delete_endpoint_url = api_url+'/delete'

mp_encoder_delete = MultipartEncoder(
    fields={
        'ids' : 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    }
)

# Let's set the headers that the delete endpoint expects.
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_delete.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to delete endpoint...")
response = requests.post(delete_endpoint_url, data=mp_encoder_delete, headers=headers)

print("Response status code: " + str(response.status_code))

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.

Source: GitHub

Breaking Down the Code

api_url = "https://api.pdfrest.com"

This line sets the base URL for the API. By default, it uses the US-based service. There is an option to switch to the EU-based service for GDPR compliance.

delete_endpoint_url = api_url+'/delete'

This constructs the full URL for the Delete Files endpoint by appending '/delete' to the base API URL.

mp_encoder_delete = MultipartEncoder(
    fields={
        'ids' : 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    }
)

The MultipartEncoder is used to encode the fields for the API request. The 'ids' field contains a comma-separated list of file IDs that you want to delete.

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_delete.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

The headers specify the content type as 'multipart/form-data', which is automatically set by the MultipartEncoder. The 'Api-Key' is a placeholder for your actual API key, which is necessary for authentication.

response = requests.post(delete_endpoint_url, data=mp_encoder_delete, headers=headers)

This line sends a POST request to the Delete Files endpoint with the encoded data and headers. It captures the response from the server.

Beyond the Tutorial

In this tutorial, you learned how to send a Delete Files API request using Python. This allows you to manage files on the pdfRest server programmatically. To explore more about what you can accomplish with pdfRest, try out all the API Tools in the API Lab. For more detailed information, visit the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.