DML (Data Manipulation Language) operations can be used to insert, update, merge, delete, and restore records. They play an important role both in Apex and Salesforce Flow. Although all the DML operations can be used in Apex, only 3 of them are available in Flow. These 3 DML operations are create, update, and delete. Delete operation can be used to delete records from the database, which sends those records to the recycle bin. Therefore, the user can go to the recycle bin and restore the deleted records. In some use cases, you may want to hard delete the records so that the users cannot restore them. However, there is no standard way to hard delete records in Salesforce Flow.
So, how can you hard delete Salesforce records using Flow?
Salesforce Flow can execute Apex actions too. If there is no standard way to perform the actions that you need, you can always achieve these requirements by calling Apex actions. This logic can be used for the hard delete operation as well.
Apex Class to Hard Delete Records
As mentioned before, Apex can be used to hard delete records. You can build your own Apex class that has an invocable method and then execute it from a flow.
At the end of the post, you can find the installation links of an Apex class (test class is also included) that receives a record collection and deletes those records for good. It means that the records don't go to the recycle bin at all.
Build a Flow to Hard Delete Contact Records
Let's build a record-triggered flow that deletes the related contact records of an account.
1- Create a new record-triggered flow that runs when an account is updated. Add a condition that it triggers only when the Active picklist field becomes "No". Select "Actions and Related Records" so that the flow will run after save and the action element will be available.
2- Add a Get Records element to get all of the related contact records according to the AccountId field.
3- Add an Action element and choose HardDeleteRecords. If you installed the package, you should have this action. This Apex action can work with any sObject. Therefore, you have to select the object first, then provide the record collection that you want to delete.
These are all you need in order to hard delete the records. At the end, your flow should look like this. Of course, you can improve or change this flow according to your requirements. On the other hand, it is always recommended to handle the errors.
Here you can see the flow in action.
- Unlike the delete operation, this Apex action doesn't fail if there are no records in the collection. Therefore, it is optional to use a decision element to check if there are records in the collection. Read this post to learn more about DML operations for an empty list in flow.
- This Apex class is actually performing two actions. It deletes the records (using the delete method), then it removes them from the recycle bin (using the emptyRecycleBin method). Therefore, as you can see, this action uses 2 DML statements. Keep in mind that the DML statements governor limit is 150 per flow transaction. On the other hand, same thing applies for the DML rows usage. Even though it deletes 2 records in the screenshot below, it uses 4 DML rows.
You can install this action (Apex class) from these links.
Production or Developer Edition Environments
Thanks for this helpful article.
I have a challenge defining the Input Value for the delete action.
Need to delete the account which is triggering the flow.
But I cannot select it, error message: "The data type of the resource you entered isn't compatible."
Can you help?
This action works with a record collection variable. So create a record collection variable of Accounts, add the current record to the collection (using an assignment element), and then provide the collection as the input of the action.