Apex
Examples
Delete uploaded files to free up salesforce storage
in this tutorial, we’ll walk you through setting up an apex trigger on the cloudfiles event object in salesforce this trigger will automatically evaluate the event type, and if the type is salesforce file copied , it will fetch all related contentdocument records that were copied to external storage and delete them from salesforce this helps free up storage space once files have been safely transferred via cloudfiles the salesforce file copied event also contains the ids of the related contentdocument records, which are used to identify and delete these files pre requisites for using cloudfiles apex in order to use cloudfiles apex, make sure you have cloudfiles installed in the correct sandbox / production environment also make sure you have authenticated your cloud drive using cloudfiles atleast once simply follow steps 1 & 2 mentioned in the installation guide docid\ fv1sn qekhvsae njyp6y and steps 1 & 2 mentioned in the connect external folder to a record docid\ inrzc0sn0m0qq8mqxnywg as well to make sure you are setup step 1 create an apex trigger on cloudfiles event object we will create a trigger that executes after an cloudfiles event record is inserted this trigger will call a helper class that deletes any contentdocument records that have been copied to external storage, freeing up salesforce storage space trigger cloudfilesobjecttrigger on cldfs cloudfilesevent c (after insert) { cloudfileseventtriggerhandler execute(); } what this does the trigger runs after a cloudfiles event record is created it calls the cloudfileseventtriggerhandler execute() method step 2 create the helper class the helper class will check if the event type is salesforce file copied retrieve all related contentdocument ids from the event payload delete the corresponding contentdocument records from salesforce we at cloudfiles have built an appexchange app to work with files easily in salesforce using cloudfiles, you can avoid setting up an api server, setting up authentications or building integrations with different cloud storage platforms below is an apex code snippet demonstrating how to delete the copied files from salesforce storage public with sharing class cloudfileseventtriggerhandler { public static void execute() { list\<cldfs cloudfilesevent c> eventrecords = (list\<cldfs cloudfilesevent c>)trigger new; list\<cldfs event> returnedevents = new list\<cldfs event>(); list\<cldfs geteventdetails flowinput> geteventdetailinputs = new list\<cldfs geteventdetails flowinput>(); for(cldfs cloudfilesevent c record eventrecords){ cldfs geteventdetails flowinput singleinput = new cldfs geteventdetails flowinput(); singleinput eventcustomobj = record; geteventdetailinputs add(singleinput); } returnedevents = cldfs geteventdetails geteventdetails(geteventdetailinputs); list\<cldfs event> sffilecopiedevents = new list\<cldfs event>(); for(cldfs event e returnedevents){ if(e type == 'salesforce file copied'){ sffilecopiedevents add(e); } } if(sffilecopiedevents size() > 0){ deletecopiedcdns(sffilecopiedevents); } } public static void deletecopiedcdns(list\<cldfs event> sffilecopiedevents){ if (sffilecopiedevents == null || sffilecopiedevents isempty()) return; list\<string> contentversionids = new list\<string>(); for(cldfs event e sffilecopiedevents){ contentversionids add(e salesforcefilecopied contentversionid); } if (contentversionids size() == 0) return; list\<contentdocument> docstodelete = \[ select id from contentdocument where id in ( select contentdocumentid from contentversion where id in \ contentversionids ) ]; delete docstodelete; } } what this does the helper class retrieves detailed event information from cloudfiles using the get event details docid\ c2sr8lnyvsqbw2wvmpbfk method it filters events of type salesforce file copied for these events, it extracts the contentversion ids of the files that were copied to external storage it queries the corresponding contentdocument records in salesforce it deletes those contentdocument records, freeing up salesforce storage space once the files have been safely stored externally parameters cldfs geteventdetails geteventdetailinputs get event details docid\ c2sr8lnyvsqbw2wvmpbfk cldfs event cldfs event event docid 9 hjkmcqcmqj8h jksr4f next steps add more checks to specifically delete files related to particular objects before deleting the files from salesforce, you can add any required logic as per your requirements this step is not limited to file deletion — you can adapt it for any other actions as per your business needs now you have an automated file deletion system in salesforce for files that were copied to external storage via cloudfiles actions!