+ Start a Discussion
Walter@AdicioWalter@Adicio 

How do I delete 1000 records at once?

I know there is a wealth of information out here on this and I am studying, but I haven't figure it out yet.

 

Do I just need to use a loop like this?

 

 

for(List<Account> a : [select id from Account where IsDeleted=false]){delete a;}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

Not quite - the syntax is slightly wrong, and more importantly, as it stands, you could add more than 1000 items to the list which would cause an error.

 

Two options:

 

 

//For a 1000 or less Account [] a = [select id from Account where IsDeleted=false limit 1000]; delete a; //or List<Account> adelete = new List <Account>{}; For (Account k : [Select Id from Account limit 10000]){ adelete.add(k); if (adelete.size() > 998) { delete adelete; adelete.clear(); } } //You need this final clean up as the final list you creates is likely less than 998 delete adelete;

 

 

All Answers

BritishBoyinDCBritishBoyinDC

Not quite - the syntax is slightly wrong, and more importantly, as it stands, you could add more than 1000 items to the list which would cause an error.

 

Two options:

 

 

//For a 1000 or less Account [] a = [select id from Account where IsDeleted=false limit 1000]; delete a; //or List<Account> adelete = new List <Account>{}; For (Account k : [Select Id from Account limit 10000]){ adelete.add(k); if (adelete.size() > 998) { delete adelete; adelete.clear(); } } //You need this final clean up as the final list you creates is likely less than 998 delete adelete;

 

 

This was selected as the best answer
Walter@AdicioWalter@Adicio
Thank you 
ThomasTTThomasTT
And, the governor limit for the number of DML records can be more than 1000 (e.g. In VF controller), so you can repeat this operation several time in 1 request. To contain more than 1000 objects, you can use List<List<Account>>. Of course, you need to execute delete for each List.
ThomasTT
Message Edited by ThomasTT on 10-02-2009 07:16 PM
Walter@AdicioWalter@Adicio
Thank you ThomasTT
MachhiMachhi
Hi, I wonder how you could update records upto 10000.  I can loop through 3500 records but I can update only 800 records using the record list object. Below is the code snippet:  

Datetime strStartDate; Datetime strEndDate; Integer BATCH_TO_PROCESS = 10; DateTime t = DateTime.now(); integer year = t.year(); strStartDate = Datetime.ValueOf(year + '-01-01 00:00:00'); strEndDate = Datetime.ValueOf(year + '-12-31 23:59:59'); Set<Id> ContactIdSet = new Set<Id>(); for(List<Contact> ContactList : [select Number_of_Cases__c, Number_of_Contracts__c, ID from Contact where createddate >= :strStartDate and createddate <= :strEndDate]) { for(Contact objContact : ContactList) { ContactIdSet.add(objContact.id); } if(intBatchCount<BATCH_TO_PROCESS) { //This is asynchronous method which processes updates BatchContactUpdate(ContactIdSet); ContactIdSet.clear(); //Increase batch count intBatchCount++; } } @future public static void BatchContactUpdate(Set<Id> pm_ContactIds) { List<Contact> contactsToUpdate = new List<Contact>{}; List<Contact> contacts = [select Id, Number_of_Cases__c, Number_of_Contracts__c from Contact where id IN :pm_ContactIds]; for(Contact c: contacts) { intCountForContact ++; if(pm_bIsIncludeNumberOfCases) c.Number_of_Cases__c = [select count() from Case where Contact.id = :c.id]; if(pm_bIsIncludeNumberOfContracts) c.Number_of_Contracts__c = [select count() from Contract where CustomerSigned.id = :c.id or Contract_Owner__c = :c.id]; //Add to contact update list contactsToUpdate.add(c); } //Update all records together update contactsToUpdate; }

 

 

  What wrong am I doing here? I have a Visualforce page with custom controller containing above code which is being called on click of the “Run” button.                                 

It throws error as “Too many SOQL queries:101” error if the contact records in the current year are more than 900 on average.  

 

What I can’t figure out is why I am getting limitation error of 100 SOQL queries even though I am using future annotation. If I do not use future annotation, I can process only 100 records.

 

Any suggestion on how to overcome this situation?   (I am using approach suggested here: http://wiki.developerforce.com/index.php/Apex_Code_Best_PracticesI am afraid that this approach is limited only for calls inside trigger. )  

 

Thanks,

Chandrakant M

Message Edited by Machhi on 10-20-2009 04:37 AM