Question
I was told that classes and triggers cannot be deleted directly in Production. Instead, they must be deleted in a Sandbox and then deployed as deletions. However, after deleting a class in Sandbox, it does not appear as a deployable change—red or otherwise.
Do deleted triggers/classes show up somewhere else for deployment? Does this process only apply to classes? Additionally, I was able to delete a class via the IDE in both Sandbox and Production. Does the deletion method depend on how the class was originally deployed?
Answer
Yes, you cannot delete Apex classes or triggers directly from Production through standard UI methods. Instead, you have to deploy deletions using metadata deployment via an IDE, Salesforce CLI, or a Sandbox-to-Production deployment process.
Method 1: Using Salesforce CLI or an IDE (Recommended for Direct Deletion)
One effective way to delete a class from Production is by using an IDE or the Salesforce CLI. Follow these steps:
- Use an IDE (like Visual Studio Code with Salesforce extensions) or Salesforce CLI to retrieve all classes from Production.
- Locate the metadata file of the class you want to delete.
- Modify the class’s metadata file (
.cls-meta.xml) by changing its status to"Deleted", as shown below:
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>23.0</apiVersion>
<status>Deleted</status>
</ApexClass>4. Save the changes and deploy them back to the Production org using the IDE or Salesforce CLI.
5. After deployment, the class will be deleted.
It’s strongly recommended to try this in a Sandbox first before attempting it in Production.
Method 2: Deleting via Sandbox and Deploying the Deletion to Production
If you prefer using a Sandbox, follow these steps:
- Delete the class or trigger in the Sandbox.
- Create an outbound change set and attempt to include the deleted class/trigger. However, deleted items may not always appear in the change set.
- If they do not appear, you may need to manually create a destructive changes XML file and deploy it using Salesforce CLI or an IDE instead.
Why don’t deleted triggers always show up in the change set?
Deleted classes and triggers do not always appear in “red” in change sets. This is because Salesforce’s UI does not explicitly track deletions in metadata deployments. Instead, you must use a destructive changes file to force the deletion.
Method 3: Using a Destructive Changes File
If neither of the above methods work, you can use a destructiveChanges.xml file to remove the class or trigger.
- Create a
destructiveChanges.xmlfile with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>ClassNameToDelete</members>
<name>ApexClass</name>
</types>
<types>
<members>TriggerNameToDelete</members>
<name>ApexTrigger</name>
</types>
<version>57.0</version>
</Package>2. Deploy this file using the Salesforce CLI with the sfdx force:mdapi:deploy command.
Deleting Apex classes or triggers from Production requires a metadata deployment approach. The easiest way is to modify the metadata file and set the status to "Deleted" before deploying it back to the org. If that does not work, you may need to use a destructive changes deployment through Salesforce CLI. It’s always best to test deletions in a Sandbox before applying them to Production.





Leave a Reply
You must be logged in to post a comment.