0

i want to delete all Acccounts in my Org that are not associated with a case in the apex console. How can i do it?

Thanks in avance

1

You can run the below query in anonymous window, which will delete all the Accounts which are not related to Cases.

set<Id> accountIds = new set<Id>();
for(Case c : [SELECT Id, AccountId from Case where AccountId !=null]){
    accountIds.add(c.AccountId);
}

delete [select id from Account where Id NOT IN : accountIds];

Another approach would be to query on Account and get all the accounts which does not have any cases associated with it

List<Account> accountsToDelete = new List<Account>();
for(Account a : [select id, (select Id from Cases) from Account]){
    if(a.Cases.size()==0){
        accountsToDelete.add(a);
    }
}
delete accountsToDelete;

UPDATE

From your comment, you have related closed/won opportunities, so you will have to delete those opportunities first before deleting the Accounts, below code should help you

List<Account> accountsToDelete = new List<Account>();
List<Opportunity> opportunitesToDelete = new List<Opportunity>();
List<OpportunityHistory__c> opportunityHistoriesToDelete = new List<OpportunityHistory__c>();
for(Account a : [select id, (select Id from Cases),(select id from Opportunities) from Account]){
    if(a.Cases.size()==0){
        accountsToDelete.add(a);
        opportunitesToDelete.addAll(a.Opportunities);
    }
}
opportunityHistoriesToDelete = [SELECT Id from OpportunityHistory__c where OpportunityId__c IN :opportunitesToDelete];

delete opportunityHistoriesToDelete;
delete opportunitesToDelete;
delete accountsToDelete;

Note that above two approaches will work only if you have total records (Account and Opportunitycombined) less than 10K with no DML statements on Account's/Opportunity's Trigger/Process Builder/Flow. If you have more than 10K records which are meeting that criteria you will have to tweak the code and run that tweaked code multiple times.

Note : It is always advisable to take the backup of the records to be deleted before actually deleting it. You can take the backup of the records using DataLoader or Workbench. Anyhow, deleted records will still be available in the recycle bin for 15 days post deleting it unless it has been cleared.

  • Thank very much @Vijay Ganji but i'm recieving this error: Line: 7, Column: 1 System.DmlException: Delete failed. First exception on row 2 with id 0012p00002NQoCCAA1; first error: DELETE_FAILED, Your attempt to delete Cobb132 Inc could not be completed because some opportunities in that account were closed won. The opportunities that could not be deleted are shown below.: Opportunity for Freeman746 : [] – forza forza Jun 11 at 7:54
  • So in that case, you will have to delete the opportunities associated with that account first and then delete the accounts. Updated the answer with the script. That should help you. – Vijay Ganji Jun 11 at 8:02
  • Thanks once more but still have following errors: System.DmlException: Delete failed. First exception on row 2 with id 0062p00000zQzgOAAS; first error: DELETE_FAILED, Your attempt to delete Opportunity for Stokes949 could not be completed because it is associated with the following opportunity histories.: a002p00000pc8cG : [] – forza forza Jun 11 at 8:09
  • I have also tried the second approach but the errors were the same like the first one – forza forza Jun 11 at 8:11
  • @forzaforza You can run the updated script, this will delete OpportunityHistory, Opportunity and then Account – Vijay Ganji Jun 11 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.