How to Delete Files On-Demand with PHP

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

Why Delete Files with PHP?

The pdfRest Delete Files API Tool is a powerful feature that allows users to remove files from the pdfRest service using a simple API call. This tutorial will guide you through the process of sending an API call to the Delete Files endpoint using PHP. By leveraging the capabilities of the Guzzle HTTP client, you can efficiently manage files stored on the pdfRest platform.

A user might use the Delete Files API to ensure that sensitive or outdated documents are removed from the pdfRest service. For example, a company that processes numerous PDF documents daily might need to delete files after processing to comply with data retention policies.

Delete Files with PHP Code Example

require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

// By default, we use the US-based API service. This is the primary endpoint for global use.
$apiUrl = "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
 */
//$apiUrl = "https://eu-api.pdfrest.com";

$client = new Client(); // Create a new instance of the Guzzle HTTP client.

$headers = [
  'Api-Key' =--> 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
];

$options = [
  'multipart' => [
    [
      'name' => 'ids',
      'contents' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
    ]
  ]
];

$request = new Request('POST', $apiUrl.'/delete', $headers); // Create a new HTTP POST request with the API endpoint and headers.

$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response.

echo $res->getBody(); // Output the response body, which contains the result

Source: GitHub

Breaking Down the Code

The code begins by requiring the autoload file from Composer, which loads the Guzzle HTTP client. This is essential for making HTTP requests in PHP.

require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

Next, the code sets the API URL to the US-based endpoint. It also provides an option to switch to the EU-based endpoint for GDPR compliance, which can be uncommented if necessary.

$apiUrl = "https://api.pdfrest.com";
//$apiUrl = "https://eu-api.pdfrest.com";

A new Guzzle HTTP client instance is created, which will be used to send the API request.

$client = new Client();

The headers are defined to include the API key, which authenticates the request. This key should be replaced with your actual API key.

$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

The options for the request include a multipart form-data body, which specifies the 'ids' of the files to be deleted. These IDs should be replaced with the actual file IDs you wish to delete.

$options = [
  'multipart' => [
    [
      'name' => 'ids',
      'contents' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
    ]
  ]
];

A new HTTP POST request is created with the specified endpoint and headers. The request is then sent asynchronously, and the response is awaited.

$request = new Request('POST', $apiUrl.'/delete', $headers);
$res = $client->sendAsync($request, $options)->wait();

Finally, the response body is outputted, which contains the result of the delete operation.

echo $res->getBody();

Beyond the Tutorial

In this tutorial, you learned how to use PHP to send an API call to the pdfRest Delete Files endpoint. This allows you to manage files stored on the pdfRest platform effectively. You are encouraged to explore all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for more detailed information.

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.