How to Permanently Delete Apex Triggers and Apex Classes from a Salesforce Production Org

Updated: Jun 9

So, you want to blow up some Apex, do ya?


Let me start off by saying, don't do this unless you know what you are doing. Salesforce doesn't allow users, not even Salesforce Admins, to delete Apex Triggers or Apex Classes from a production environment. At least not out-of-the-box. There is good reason to limit these abilities. There are tools available to developers, such as Salesforce DX and ANT that can make the process less painful, but for most non-developers, navigating these tools is more difficult than useful.


First Step. I always recommend first commenting out all of the code in an Apex Trigger and corresponding Apex Classes and pushing those to Production. By doing so, you retain the code but the code essentially does nothing. This will give you some time to determine how well your org responds to the change, just like any change set. To comment out all code in a class, see the example below:


Before we comment out the code:

public class OpportunityHandler {

< here is where all your code is inside the Apex Class >

}


After we comment out the code:

public class myApexClassToDelete {

/*

< here is where all your code is inside the Apex Class >

*/

}


After the code is commented out, push it via a change set. Validate it and deploy. After confirming the org is continuing to function as expected, proceed with the permanent deletion.


Open a Notepad file and paste the following code:

<?xml version="1.0" encoding="UTF-8"?>

<Package xmlns="http://soap.sforce.com/2006/04/metadata">

<version>30.0</version>

</Package>


Save the file as 'package.xml'.


Open another Notepad file and paste the following code:

<?xml version="1.0" encoding="utf-8"?>

<Package xmlns="http://soap.sforce.com/2006/04/metadata">

<types>

<members>myApexClassToDelete</members>

<name>ApexClass</name>

</types>

<version>30.0</version>

</Package>


Now, you will need to change the value of the members tag to the name of the Apex Class you would like to delete. If you are deleting an Apex Trigger, you will need to change the value of the name tag to ApexTrigger. You can only delete ApexTrigger or ApexClass types per deletion. You cannot do both at the same time. If you want to delete more than one ApexTrigger or more than one ApexClass at a time, you need to add a new members tag for each. For example:


<?xml version="1.0" encoding="utf-8"?>

<Package xmlns="http://soap.sforce.com/2006/04/metadata">

<types>

<members>myApexClassToDelete1</members>

<members>myApexClassToDelete2</members>

<members>myApexClassToDelete3</members>

<members>myApexClassToDelete4</members>

<name>ApexClass</name>

</types>

<version>30.0</version>

</Package>


Save the file as 'destructiveChanges.xml'.


Select both files so they are highlighted, right-click and select Send To / Compressed (zipped) Folder. Name the new zip file 'package'.


Log into Workbench by going to https://workbench.developerforce.com/ and signing into the Salesforce Production org you are targeting.


After logging in go to the Migration/Deploy.


Click the 'Choose File' button and select the zip file 'package' you just created. Check the box next to 'Rollback on Error' and 'Single Package'. Set the Test Level to 'RunSpecifiedTests'. In Run Tests, paste in the name of the ApexTrigger or ApexClass you are trying to delete.


Note: If you are trying to delete more than one ApexTrigger or more than one ApexClass at a time, you need to paste in the FIRST of these in the members tag into the Run Tests field.


Click Next.


After reviewing the notes, and confirming you want to delete (THERE IS NO GOING BACK), click Deploy.


Workbench will now attempt to delete the code. This can take a couple minutes to do, especially if it is deleting multiple.


Common Errors when Deploying include 1) Incorrect spelling of ApexTrigger/ApexClass in the RunSpecifiedTests field and 2) Deleting Apex that is called by another piece of Apex NOT being deleted. The latter is like trying to delete a field that is referenced in a formula field. You can't do it until you delete the formula field or remove the field you want to delete from that formula field.


Remember: This method makes permanent changes in your Production org so don't do this unless you know what you are doing.


Happy deletion!


To get started with Cloud Pacific, go here to complete a simple form. A Cloud Pacific Account Manager will collaborate with you to clarify your needs and goals, and customize a service package and roadmap that will help your business grow and thrive on Salesforce. Reach out to us here!

19 views0 comments