How to Delete Files Uploaded as Attachments in Pipedream Workflow?

This topic was automatically generated from Slack. You can find the original thread here.

How we can delete files which gets uploaded as attachments in the pipedream workflow, didn’t find any options there?

To delete files uploaded as attachments in a Pipedream workflow, you can use a code step with the Pipedream API. First, you’ll need to get the ID of the attachment you want to delete. Then, you can call the Pipedream API to delete the attachment. Here’s an example code step to delete an attachment: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { attachmentId: { type: "string", label: "Attachment ID", }, pipedreamApiKey: { type: "string", label: "Pipedream API Key", }, }, async run({ steps, $ }) { const apiUrl = `https://api.pipedream.com/v1/attachments/${this.attachmentId}`; const headers = { Authorization: `Bearer ${this.pipedreamApiKey}`, }; try { await axios($, { method: "DELETE", url: apiUrl, headers: headers, }); console.log("Attachment deleted successfully"); } catch (error) { console.error("Error deleting attachment:", error); } }, }); Replace attachmentId and pipedreamApiKey with the appropriate values. You can find your Pipedream API key in your account settings.

How to find the attachment ID from the workflow

To find the attachment ID from a workflow, you’ll need to access the event data that contains the attachment information. The exact structure of the event data depends on the trigger you’re using. For example, if you’re using an HTTP trigger and receiving a file as a form-data attachment, you can access the attachment ID like this: export default defineComponent({ async run({ steps, $ }) { const attachment = steps.trigger.event.attachments[0]; const attachmentId = attachment.id; console.log("Attachment ID:", attachmentId); }, }); This code assumes that you have an HTTP trigger named trigger and that the incoming request contains a file attachment. The attachment ID is extracted from the first attachment in the attachments array. Make sure to adjust the code according to your specific trigger and event data structure.

I have uploaded the attachments inside the workflow, how we can fetch the attachment id?

To fetch the attachment ID of a file uploaded in the Attachments section of your workflow, you can access it through the steps.trigger.context.attachments object. However, this object provides the file path, not the attachment ID. Here’s an example of how to access the file path of an uploaded file named test.csv: ```
import fs from “fs”; const filePath = steps.trigger.context.attachments[“test.csv”]; console.log(“File path:”, filePath); const fileData = fs.readFileSync(filePath).toString(); console.log(“File data:”, fileData);