I am having a doubt in Salesforce.

I know the difference between Look up and master-detail relationship in Salesforce.

But Suppose I am having a scenario, where I am having 2 object with look up relationship. I want that on deletion of parent object, the child object automatically be deleted.

How I will achieve this?

Please remember here the relation is lookup.


  • 1
    I fear it will be a trigger Commented Nov 11, 2013 at 6:38
  • 2
    Yes it's a trigger, unless you're able to recreate the relationship as a master-detail. But as a lookup you're going to need a before or after delete trigger to clean up the orphans.
    – Adam
    Commented Nov 11, 2013 at 6:44
  • @Adam : Yes you are right. Solution for this type of scenario is trigger. But I was looking for other solution to achieve this. So, as per Mohit and you I believe there is no other solution other than writing Trigger. Commented Nov 11, 2013 at 6:52
  • it will be after delete trigger. nothing else Commented Nov 11, 2013 at 8:58
  • Worth noting that with newer Salesforce releases this can apparently be done without an Apex Trigger. See jenwlee.wordpress.com/2016/10/25/…
    – Phil W
    Commented Aug 31, 2018 at 10:44

2 Answers 2


If its a lookup you will need a simple trigger to do this in real time

trigger onParentObjectDelete on CustomObject__c (before delete){
List<Id> idsToQuery = new List<Id>{};
for(CustomObject__c a: Trigger.old){

//query all child records where parent ids were deleted
ChildObject__c[] objsToDelete = [select id from ChildObject__c where ParentId__c IN :idsToQuery];

delete objsToDelete; //perform delete statement

This is if you are planning to do a real time delete .Also in this case be sure to check with business what if someone undeletes a record from the bin.May be a trigger to handle this as well

Also if you don't need a real time delete ,a batch process to fetch orphaned childs(ParentId is null) and delete .

  • 1
    Kumar : Thanks Mohit for this code. It is really helpful for me. Commented Nov 11, 2013 at 6:54
  • Mohith and others- the problem with doing this in the before delete trigger is that if two parent records are merged, the children of the losing parent record are deleted before the merge. But in after delete, in the non-merge scenario, the relationship has disappeared and we can't query for children based on the deleted parent's ID. Any way to get around this? Commented Oct 25, 2016 at 15:03
  • And by the way there is no Trigger.new context variable in a delete trigger. Only Trigger.old exists. Commented May 7, 2021 at 12:59
  • Thanks! Fixed it Commented May 7, 2021 at 13:01

I had to solve a similar case; I have a lookup relationship between objects - it cannot be a master-detail - and need to make sure all the related objects get deleted when the "parent" is deleted.

The solution I employed was to gather a list of the IDs of the children for all deleted parents (in a bulkified manner) during "before delete" in a trigger. This was stored in a static variable. I then get the details out of the static variable (and clear it, to avoid re-entrant calls to the trigger messing stuff up) before deleting the child objects in "after delete".

I don't know how this will work for record merging, since this isn't relevant in our case, though I dare say the latter could be handled by checking the relationship field (the lookup) on the children to be deleted just before they are deleted...

An alternative solution might be to do something like that described here: https://jenwlee.wordpress.com/2016/10/25/delete-a-record-deleted-its-related-records/

I used a trigger since I needed fine-grained control of related processing as well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .