How to Delete Files On-Demand with JavaScript in NodeJS
Why Delete Files with JavaScript?
The pdfRest Delete Files API Tool is a powerful feature that allows users to remove files from the pdfRest server efficiently. By using JavaScript, developers can integrate this functionality directly into their web applications, enabling seamless file management. This tutorial will guide you through sending an API call to the Delete Files endpoint using JavaScript, demonstrating how to delete multiple files in one request.
A user might need to delete files to maintain data privacy. For instance, a document management system could use this API to automatically remove outdated or unnecessary files from the server, ensuring that only relevant documents are stored and accessible.
Delete Files with JavaScript Code Example
// This request demonstrates how to delete multiple files at once var axios = require("axios"); var FormData = require("form-data"); // Create a new form data instance and append the PDF file and parameters to it var data = new FormData(); data.append( "ids", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ); // define configuration options for axios request var config = { method: "post", maxBodyLength: Infinity, // set maximum length of the request body url: "https://api.pdfrest.com/delete", headers: { "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key ...data.getHeaders(), // set headers for the request }, data: data, // set the data to be sent with the request }; // send request and handle response or error axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); }); // If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.
Source: GitHub Repository
Breaking Down the Code
The code begins by importing the necessary modules, axios
for making HTTP requests and FormData
for constructing form data payloads:
var axios = require("axios"); var FormData = require("form-data");
Next, a new FormData
instance is created, and the IDs of the files to be deleted are appended to it. These IDs are unique identifiers for the files stored on the server:
var data = new FormData(); data.append( "ids", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" );
The configuration for the axios
request is defined, specifying the HTTP method as post
, setting the URL to the Delete File endpoint, and including headers. The Api-Key
is a required header for authentication:
var config = { method: "post", maxBodyLength: Infinity, url: "https://api.pdfrest.com/delete", headers: { "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", ...data.getHeaders(), }, data: data, };
Finally, the request is sent using axios
, and the response or any errors are handled with then
and catch
methods:
axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
Beyond the Tutorial
In this tutorial, you learned how to use JavaScript to call the pdfRest Delete Files API, enabling you to delete multiple files from the server. This functionality is crucial for efficient file management and can be integrated into various applications to automate file deletion processes.
To explore more about pdfRest's capabilities, try out all the API Tools in the API Lab. For detailed information on each API, refer to the API Reference Guide. Note that this example demonstrates a multipart API call, and code samples using JSON payloads can be found here.