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
venkateshvenkatesh 

Mass delete problem

 Hi,

 

if ((purgeType == 'LAST_N_DAYS') && (nDays <= 365)) {
List<ConnectCompiere_Log__c> lastNDays = new List<ConnectCompiere_Log__c>([SELECT Id FROM ConnectCompiere_Log__c WHERE CreatedDate >: System.now().addDays(-nDays)]);
// for(ConnectCompiere_Log__c rec:lastNDays) {
// delete rec;
// }
System.debug('Selecting' + lastNDays.size() + ' log records');
delete lastNDays;
}

 

Im getting the below error for the above code. 

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Purging caused an unexpected exception, contact your administrator: Purging: execution of AfterInsert caused by: System.Exception: Too many DML rows: 440: 

 

Above code selecting 440 rows to delete from the object. While deleting I am getting above error.

 

 Please help me to delete the mass records.

 

Thanks in advance.

 

Please Please help me!!!!!!!!!!!!!!!!!! :smileysad: 

Message Edited by venkatesh on 03-03-2009 12:32 AM
Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

I think you need to use @future annotation in a separate apex class. So instead of executing the delete statement in the trigger, move at minimum the Delete DML statement to a separate apex class, specifically to a method annotated with the @future keyword. That will loosen the governor limits, but also run the delete asynchronously.

 

All Answers

aalbertaalbert

You are trying to delete too many rows and running into a governor limit. How is this code invoked? By a trigger? And how many records are invoking the apex? Just a single row? For example, if a single record initiates an apex trigger, you can only process 100 records in a DML statement (ie delete). That number increases if more than 1 record initiates the trigger. 

 

You could try moving the delete logic to an apex method declared with the @future annotation. You get higher governor limits but the apex runs in async mode. 

 

Here is documentation on the governor limits.

venkateshvenkatesh

Thanks for your quick reply Aalbert.

 

A single record initiates an apex trigger. This is true as per the Governer limit that it will delete only 100 records per trigger. But I will not be writing any metod inside the trigger. Then how and where I can put the  @future annotation?

 

Also I got some information in one of the blog that "The limits are put in place by Salesforce.com to protect their multi-tenant architecture from being slowed down by individual users. Specifically, Triggers are only allowed 20 DML calls (insert, update, upsert, merge, or delete). There are various Bulk-Safe methods to avoid these limits but my Trigger was just too complex for such trickery."

 

   How I can achieve other Bulk-Safe methods to avoid the limitation and successfully achieve bulk deletion of the records. 

 

Any one who knows please please help me.............. :smileysad:

Message Edited by venkatesh on 03-03-2009 09:58 PM
jrotensteinjrotenstein

You might also be calling delete too many times.

Rather than calling it with a single record, you could create a list of the records you wish to delete, then calling delete on the list. This will reduce the number of DML commands you are issuing.

venkateshvenkatesh

I am not getting you exactly. I have created a list to be deleted from object, then I have directly deleted the list in the above example. But If I call the delete inside the For loop, then it will gives error and also If I remove the For loop, then also gives the error.

 

It woould be very helpful, If you are providing any code sample for calling delete on the list. I am struck in this operation. Please help me.

 

Thanks for your quick reply.

Please help me to delete the records in batch.......... I am looking for any sample.

Please!!!!! Please!!!!

Message Edited by venkatesh on 03-04-2009 01:21 AM
jrotensteinjrotenstein

Ah, now I understand. You were trying it both ways.

It might be easiest to limit the number of rows you are deleting in each execution, but have the code run more often.

Here's an example of Mass Delete via the System Log window.

venkateshvenkatesh

I tried from System Log Window. That is working fine. But I would like to add that code in my trigger. Unfortunately that is not working in the trigger code.

 

I am expecting to delete the records of the object on the basis of created date(For example selecting last 90 days records or selecting last quarter records). That object may contain more than 1000 records.

How I can delete those records. Please suggest me.

 

Thanks for your quik reply........

Please help me!!!!!

  :smileymad:  :smileymad: :smileysad::smileysad:

Please help me one who knows about Bulk-Safe methods to avoid the limits.

Message Edited by venkatesh on 03-04-2009 02:49 AM
Message Edited by venkatesh on 03-04-2009 05:37 AM
aalbertaalbert

I think you need to use @future annotation in a separate apex class. So instead of executing the delete statement in the trigger, move at minimum the Delete DML statement to a separate apex class, specifically to a method annotated with the @future keyword. That will loosen the governor limits, but also run the delete asynchronously.

 

This was selected as the best answer
venkateshvenkatesh

Thanks a lot to Aalbert.

 

Your idea is working for me.

 

Thank you very much.............

 

:smileyhappy: :smileyhappy: