When attempting to delete Apex Classes & Triggers from Production, you can be faced with a number of issues. This is due to the fact you cannot modify Apex code directly in production.
Within this article, I will go through a lightweight, and flexible solution, that enables you to delete Apex classes and triggers from production. Quickly, and easily.
The most common approach to deleting Apex classes and triggers in a Salesforce production environment is to leverage either the Force.com IDE or the Force.com Migration tool. These tools have a number of downsides, namely.
- The Force.com IDE is very ‘heavyweight’ and is known for being quite buggy sometimes and unpleasant to use.
- They have a number of dependencies (a compatible version of Java, different IDE version etc)
- Connectivity to Salesforce via the Force.com IDE may be an issue
- The Force.com migration tool (ANT) can take some time to learn how to use properly.
Apex classes and triggers cannot be deleted from a Salesforce production declaratively, unlike in sandbox orgs. This is because security for Apex in Salesforce production orgs is greatly increased. So how can one delete Apex classes and triggers from a production org without having to go through the inconvenience and time consuming effort of installing, testing and learning how to properly use these tools?
The answer can be to use Notepad text editor and the super lightweight and easy to use Workbench suite. Using these tools, deleting Apex classes and triggers from Salesforce production is a breeze. Because Workbench is web-based and text editors are already pre-installed and super easy to use, there’s no time spent on downloading, installing or testing the Force.com IDE or Force.com Migration tool (ANT).
Let’s say that you have a Salesforce production org that has two Apex classes that need to be deleted.
- To achieve this via Workbench, create a folder on your desktop. I will call my folder ‘deleteClasses’.
- Then go to Notepad (or another text editor) and copy and paste the below and save as the file with ‘package.xml’ and ‘All files (*.*)’.
<!--?xml version="1.0" encoding="UTF-8"?--> <package xmlns="http://soap.sforce.com/2006/04/metadata"> <version>30.0</version> </package>
3. Then create a new file in Notepad (or another text editor) and copy the below into it:
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://soap.sforce.com/2006/04/metadata"> <types> <members>SomeClass</members> <name>ApexClass</name> </types> <version>30.0</version> </Package>
Replace SomeClass with the name of your class that you wish to delete. If you have two classes that need to be deleted at the same time, you can simply add another <members> row into the file with the name of the other class, for example:
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://soap.sforce.com/2006/04/metadata"> <types> <members>SomeClass</members> <members>SomeOtherClass</members> <name>ApexClass</name> </types> <version>30.0</version> </Package>
4. Save this file name as destructiveChanges.xml (note the capital ‘C’ in ‘changes’). Make sure the file is saved as ‘All files (*.*)’. More information on destructive changes can be found here.
5. Now there are two files in your folder. Open the folder, select both the XML files, right-click and select ‘Send To > Compressed Folder’. Keeping the default name of ‘package’ for the folder is fine.
6. You are now setup to deploy the destructiveChanges.xml file to Salesforce. To do this, go to Workbench and login with your credentials. It is recommended that you login to a Sandbox instance before you deploy the file to production.
7. Select Migration > Deploy.
8. Click ‘Browse’ and select the .zip package file. Then check ‘Rollback on Error’, ‘Single Package’, and select Test Level with ‘RunLocalTests’. More information on the Test Level can be found here.
9. Finally, select ‘Next’ and then you will notice that the success or error results will be displayed in Workbench once the deployment process has been completed.
This is a very easy way to delete Apex classes and can be very handy if you need a lightweight tool to do the job.