Restore deleted test classes in a Salesforce managed package

Restore deleted test classes in a Salesforce managed package

When you release a Salesforce app as a managed package, Salesforce restricts a number of permissions on the classes, objects, and other contents of the package. One of these restrictions is deletion of a class – that is, you cannot delete a class that has been added to a managed package.

Or, that’s almost the case. For some reason, it is still possible to delete test classes from a managed package. And once you delete a test class, you are not allowed to re-use its name.

Since test classes are usually named according to convention (e.g. PandaTest contains tests for the Panda class), this can create unsightly naming inconsistencies. If you accidentally deleted PandaTest, not only would you have to rewrite all the tests, you would also have to break the naming convention by calling the test class something like PandaTest2, which makes the mistake permanent.

Sometimes accidents happen. Fortunately, this is one you can fix!


Just like your Accounts and Opportunities, classes are stored in the Salesforce database. They’re called ApexClass objects. But unlike most Salesforce objects, when a class is deleted in the Salesforce UI, it is not removed from the database, at least not immediately. Instead, the Status field is updated to “Deleted”, and the class is no longer shown on the classes page.

The ApexClass record still exists, however, and you’re just an ID away from recovering it!

To get the ID, you need to execute some SOQL and see the results. One way to do that is with the Salesforce developer console.

Let’s say you want to recover the PandaTest class. The query will look like this:

SELECT Id FROM ApexClass WHERE Name = 'PandaTest'

So, if you use the developer console, execute this Apex code to get the ID as debug output:

System.debug([SELECT Id FROM ApexClass WHERE Name = 'PandaTest'].Id);

Once you have the ApexClass ID (which should begin with “01p”), put it into the url to access the record.

https://[your-full-salesforce-domain]/[your-id]

Click the Edit button on the class detail screen, then save the class (no
changes to the class are necessary). Voila, everything should be back to normal.

–SC

Read more about Salesforce Development