How to Delete Files On-Demand with Python
Delete pdfRest Resources Immediately with Python
Files submitted to the pdfRest Cloud API are automatically removed after the retention period for the applicable plan, which is 30 minutes for most plans. Some applications need to remove resources sooner because a workflow has completed or an internal data-handling policy calls for immediate cleanup. The pdfRest Delete Files API Tool permanently deletes uploaded inputs and generated outputs on demand through the /delete endpoint.
On-demand deletion gives the application explicit control over the end of a resource's processing lifecycle. It should be used deliberately: once a resource is deleted, its ID cannot be used in another pdfRest call and the file cannot be recovered from pdfRest. The application must save every required output and finish all dependent operations before cleanup begins.
For example, a Python loan-processing service can upload an application package, run OCR and extraction, save the approved results inside the lender's document system, and then delete the source PDF and intermediate resources immediately. Tracking the resource IDs created at each stage lets the service clean up the complete processing set after confirming that the final records were stored successfully.
This example uses requests and MultipartEncoder to send two resource IDs for deletion in one call.
Python Delete Files Code Example
Install requests and requests-toolbelt, then replace the API-key and sample-ID placeholders with values from earlier API calls made with the same API key.
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: pdfRest Delete Files multipart Python sample
Submit the Resource IDs to Delete
The ids field accepts a comma-separated list, allowing an application to delete several related resources in one request. These may include original uploads and outputs generated by other pdfRest tools. Each resource must belong to the API key making the delete request; an ID from another account or key cannot be removed by this call.
Build the list from the application's tracked workflow state rather than accepting arbitrary unvalidated IDs. Deduplicate it, exclude any resource still needed by a later operation, and make the cleanup action idempotent at the application level so a retried job can recognize that a resource has already been removed.
MultipartEncoder supplies the multipart body and boundary. The headers authenticate the request and ask for a JSON response. The sample's configurable base URL supports the US Cloud API and the EU endpoint; use the same region where the resources were created.
Confirm Completion Before Cleanup
Do not delete an input as soon as an earlier call returns if another asynchronous or chained step still references it. Wait for the required operations to finish, verify that every final output has been downloaded or stored, and only then send the cleanup request. For workflows that fail partway through, a finally block or background cleanup job can remove resources that are no longer needed while preserving enough state to avoid deleting active work.
Check the HTTP status and response before marking an ID as deleted in the application's records. Production code should set an appropriate timeout, catch connection exceptions, and avoid logging API keys, document names, or sensitive payloads. Treat deletion as a security-relevant and irreversible operation.
The same request can be sent as JSON when that fits the application's request model. See the official Delete Files JSON-payload Python sample. Use API Lab to inspect the request and consult the Delete Files API reference for the current input and response fields.