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

How can I restore a deleted class?



I had a test class "TestRecordTypes" that is part of a managed package. I deleted the class because I wanted to move the test methods inside the controller that was being tested. This didn't work like I thought it would, so I wanted to restore the code from the deleted class, which I had saved locally.


There seems to be no way to overwrite or undelete the class. In Eclipse if I create a new class w/ the same nameI get prompted w/ "Duplicate Component Found," and it asks me if I want to overwrite the remote instance. If I choose "OK" I get a local class w/ the deleted code in it. When I save I get errors for the class and it's meta.xml file saying "Save error: Conflict found while preparing to save 'TestRecordTypes.' to server.  Remote instance has been updated since last save or sync.  Use the Synchronize Perspective to resolve the conflict." But the Synchronize with Server tool doesn't let me upload the code to the server. If I try to deploy the newly created class to the server I get an error "Class is deleted". The deleted class still shows up in the SF UI under Develop / Apex Classes but there doesn't appear to be any way to undelete the class.


So I appear to be stuck in that Twilight Zone episode where you walk down a street but always end up back where you started. Is this a "feature" or is there a way to restore the class?


thanks in advance,


Did you find any solution to this? I've recently managed to somehow delete a ApexClass and now I am unable to replace it with a previous version.
Mario Di GenioMario Di Genio

Hi, I know this question has been up for a while, this happened to us with a trigger recently and the solution we found is this:


In the schema in Eclipse we retrieve from ApexTrigger all triggers (there you will see all active, inactiva and also DELETED triggers). With the id of the trigger, we put it in the browser, for example,<the id> and that's how we were able to navigate to the class in the org. It allowed us to edit it and activate it, and after saving the trigger appeared again listed and even in the managed package that we had.


We tried to do all of this editing the meta file of the trigger changing Deleted for Active but it didn't work, but from the org as explained above it worked perfectly.


You can do the same with a class.


Hope this helps.




My God, thank you very, very much!


At first i didn't quite understand your solution, so i will clarify it here (for a class):


Open up IDE in Eclipse

Open the 'Execute Anonymous' window

Enter this code (or something like it), replacing 'DeletedClass' with the name of the class you want to restore:


ApexClass deletedClass = [SELECT id FROM ApexClass WHERE name = 'DeletedClass'][0];



Run it and find the id of the class in the Debug output in the 'Execute Anonymous' window

Append the id to the base URL of your Salesforce environment in your browser e.g. :<paste id here>


Then you can Edit and Save the class.




I tried running this and got a a 'list out of bounds' exception?


Hmm, that's interesting. I've since found out about Salesforce's 'delete log':


How long before you tried this had you deleted the Class? I think when i did it, it was on the order of a few hours to a day. Could you try again today and let me know what happens? If it's somehow not in the delete log you won't be able to retrieve it unfortunately.


This is good info to know though, as i also wrote my solution in a blog post :)


Is there a way to restore changes to a file....One of my VF pages was overwritten by mistake. Can I restore it?