I've deleted 260k contact records and want to undelete them via developer console. I tried this:

undelete [SELECT Id FROM Contact WHERE IsDeleted = true ALL ROWS];

but when I click Execute I get the following message:

Line: 1, Column: 1 System.LimitException: Too many query rows 50001

any idea on how I could do this in batches instead?


1 Answer 1


You just need to run the same script over and over again:

undelete [SELECT Id FROM Contact WHERE IsDeleted = true LIMIT 10000 ALL ROWS];

This is what I do in the Developer Console, it's easy as pressing Ctrl+Alt+E for as many times as I need. Adjust the LIMIT if you're getting CPU errors, etc.

  • thank you so much! I will try this - I'm an admin and I don't know how to properly use the dev console.
    – navreet
    Commented Nov 25, 2021 at 8:37
  • will this also restore the records properly, including related records like activities etc?
    – navreet
    Commented Nov 25, 2021 at 8:44
  • I tried this with a small batch to just test it - and ran into the following error: Line: 1, Column: 1 System.DmlException: Undelete failed. First exception on row 1 with id 0034H00001pXw3lQAC; first error: DUPLICATE_VALUE, duplicate value found: TR1__External_Candidate_Id__c duplicates value on record with id: 0034L00000MhYRH: []
    – navreet
    Commented Nov 25, 2021 at 8:47
  • @navreet That error means that you have records that already match a particular value, so it cannot be undeleted. Some additional work may be required to restore the data beyond just undeleting. And yes, undeleting a record will also undelete all related records that were deleted as a consequence of deleting the parent record.
    – sfdcfox
    Commented Nov 25, 2021 at 13:48

You must log in to answer this question.

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