Hard Delete Salesforce Records Using Flow

Hard Delete Salesforce Records Using Flow

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.

Start configuration

2- Add a Get Records element to get all of the related contact records according to the AccountId field.

Get related contact records

3- Add an Action element and choose Hard Delete Records. 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.

Add Hard Delete Records Action
Use Hard Delete Records Action

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.

Record-Triggered Flow to Hard Delete Contacts

Here you can see the flow in action.

Considerations

  • 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.
Debug Logs of the Flow

Installation

You can install this action from these links.

Production or Developer Edition Environments

Sandbox Environments

3 Comments

  1. 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?

    • Hi Stefan,
      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.

  2. This is great!
    Is there a way to make the records inaccessible via api as well?
    I found that even after hard deleting them I can still access them using isDeleted=TRUE.
    Any suggestions?

1 Trackback / Pingback

  1. How to Delete Records in Flow - Salesforce Time

Leave a Reply

Your email address will not be published.


*