function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
switi lokhandeswiti lokhande 

Whenever new record is created into account object before this new record is inserted into account delete all the contact record with this account name drecord

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Switi,

Can you try the below Trigger on Account object.
 
trigger AcountConDelete on Account (before insert) {

   set<String> namestr= new Set<String>();
    for(Account acc:Trigger.new){
        namestr.add(acc.name);// Adding name to set
    }
    
    List<Contact> tobedeleted= [select id,name from contact where name in :namestr];//quering the contacts with that name
    if(tobedeleted.size()>0)
    delete tobedeleted;
    
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,