Context: We would like to delete multiple records in the Case and its related/child objects. The child objects have few related objects. There are 4 to 5 levels of hierarchy as follows
-----------Child3
The related objects are having master-child relationship with cascade delete set to false.
Currently the way we are deleting cases in a batch is as follows
Then delete each set of records in batch using bulk delete. The advantage is we will have only 5 deletes per batch and we don't hit governor limits.
However the down side of this process is, when there is error in deleting in any of the steps above, whole transaction is rolled back. Though we can get which delete caused he error, we cannot role back objects related to only that particular case.
Question:
Use bottoms up approach along with batch.
For example start with Child3. collect all the records that needs to be purged in each related object and case object. Then delete in batch.
There are 2 ways:
Declarative method: Use the Process Event and Process Builder/flow field count in each object is less than 350. The advantage of this method is you don't have to write code.
Using Apex class: Write apex code to get all the related objects that needs to be purged/deleted and execute in batch.
a) Set the batch size of 1 so that any error while deleting, only the related records will be rolled back.
b) If batch size is set to higher than 1, whole batch will be rolled back. In this case you need to identify the parent (Case object) id and mark them for error and re-read all the other records which were rolled back as part of the batch and run again. In this method, identifying the failed record (if the records failed in child3) and its root parent (case) id could be challenging.