Salesforce Flow can be used to manipulate data. Unlike the other automation tools like Workflow Rule and Process Builder, there is an option to delete records in Flow. You can use the Delete Records data element to delete one or multiple records in Salesforce.
Which flow types support the Delete Records element?
Delete Records data element is available in most of the flow types except for the before save record-triggered flows. Before save record-triggered flows run before a record is created, updated, or deleted. This flow type can be used to update the triggering record and it doesn’t support other data elements or actions. However, there is one exception. Record-triggered flows which are working before a record is deleted support other data elements as well. So it means that if your record-triggered flow is running before a record is deleted, you can use the Delete Records data element. Read this post to learn more about the difference between before save and after save record-triggered flows.
How to Delete Records in Salesforce Flow
First of all, you have to add the Delete Records data element to the canvas. This data element has 2 options to delete records.
The first one is using a record variable or record collection variable. If you want to use this option, you have to provide a record variable or record collection variable that contains the records that you want to delete. Pay attention that your variable must contain the Id of those records. At the other hand, it also means that this action deletes the records according to their Ids. So it is enough that your record variable or record collection variable contains only the Ids and not any other field of the record.
As you can see from the picture below, you can delete the records that you got using the Get Records action.
The second option is specifying conditions to delete records. If you choose this option, then you have to select the object and then define the conditions. Therefore, you don’t have to use Get Records element. It will delete all the records that meet your criteria.
For example, this action deletes all the case records that are related to the account that triggered the flow.
Best Practices and Tips
1- This action is a little risky. At the end, it is deleting some records and you should be careful when building your flow. You don’t want to delete something wrong. So, pay attention to your criteria.
2- This is one of the DML actions that Salesforce flow supports. It consumes from the DML governor limits. Don’t forget that DML statement limit is 150, which means that you cannot have more than 150 create/update/delete records elements in your flow. At the other hand, total number of records processed as a result of DML statements is 10,000. It means that if you try to delete more than 10,000 records in a single flow transaction, then your flow will fail.
3- Just like any other data element (Get, Create, Update Records), you shouldn’t use this element inside a loop. Instead, use an assignment element in the loop and then use this action at the of your loop. Read this post to learn more about how to work with collections and loops.
4- If there are no records in your variable, or if there are no records that meet your criteria, then this action will fail. So it means that if there is no record to delete, this action fails. Because of this, always add a decision element to check if there are records before trying to delete them. If there are no records, then you can skip this action.
Read this post to learn more about DML operations for an empty list in Flow.
5- If you are using the second option (specify conditions), then this action will not just delete the records but it will also query them. It means that the action will consume from SOQL limits as well.
Additional Actions Related to Delete
1- This standard element deletes records from the database and the records go to the recycle bin. Therefore, users can go to the recycle bin to restore them. There is no standard option to hard delete the records, but you can use an action that I built in order to hard delete them. It means that the records will not go to the recycle bin. However, you have to be extra careful since there is no way to restore those records. Don’t activate the flow if you are not 100% sure about it. Read this post to learn more about the hard delete action. You can find the installation link as well.
2- Standard data elements of flow are limited to create, update, and delete. However, you can use custom invocable Apex actions to perform the remaining actions. For example, you can use this action to undelete records from the recycle bin.