· tutorials · 4 min read

Delete Apex Classes in Production (3 Methods)

Learn three methods to delete Apex Classes in Salesforce production environments, reduce technical debt, and ensure smooth deployments.

Learn three methods to delete Apex Classes in Salesforce production environments, reduce technical debt, and ensure smooth deployments.

Introduction

Deleting Apex Classes can help reduce technical debt and remove conflicting code. But deleting code in production is impossible if you don’t know the steps.

In a sandbox, we can simply use the UI to delete desired code by accessing the Apex Classes menu in Setup. However, these options do not exist in production.

This means we need to use backend tools like the Metadata API to perform the deletion. The fastest way to do all of this is using the command line, but I’ll show you three ways to delete Apex code. These methods range from UI-friendly to terminal-only.

Why Delete Apex Code?

Before we dive into the methods, let’s talk about why you want to delete Apex code. There are three main reasons:

  • Code Coverage
  • Technical Debt
  • Deployment Conflicts

Deployment Conflicts: If your code overwrites existing features, the only path forward is to delete the old code.

Technical Debt: One common mistake is leaving unused code in production, which can lead to:

  • Confusion about the purpose of the code.
  • Future deployments failing due to test coverage issues.

Code Coverage: Salesforce requires 75% test coverage across all Apex classes, measured in lines of code. Additionally, all Apex Triggers need at least 1% code coverage. Test classes need to be run against any Apex trigger.

For example, if you have two classes of equal size, and an unused class has 50% code coverage, you would need 100% coverage on the new class to achieve the 75% org-wide requirement.

Inactive triggers can be marked as such temporarily, but they still count against your code count limit and can’t be tested. If you’re not using your code, delete it. If you’re worried about needing it down the line, that’s what git is for.

Method 1: Using Workbench (UI-Only)

Step-by-Step Instructions

This method is entirely UI-based.

  1. Create Deployment Folder: Organize your deployment package by creating a new folder deployment.

  2. Create package.xml: Specify the Salesforce deployment.

    <!--?xml version="1.0" encoding="UTF-8"?-->
    <package xmlns="http://soap.sforce.com/2006/04/metadata">
        <version>59.0</version>
    </package>
    
  3. Create destructiveChanges.xml: List the files to delete.

    <?xml version="1.0" encoding="utf-8"?>
    <Package xmlns="http://soap.sforce.com/2006/04/metadata">
        <types>
            <members>EngagementTriggerTest</members>
            <members>EngagementTriggerHandler</members>
            <name>ApexClass</name>
        </types>
        <types>
            <members>EngagementTrigger</members>
            <name>ApexTrigger</name>
        </types>
        <types>
            <members>TestPage</members>
            <name>ApexPage</name>
        </types>
        <version>59.0</version>
    </Package>
    
  4. Zip the Files: Use the standard zip tool to create package.zip. Ensure you zip the files together, not the containing folder.

  5. Deploy with Workbench:

    • Log in to your production org.
    • Navigate to “Migration” -> “Deploy”.
    • Upload the zip file.
    • Check “Rollback on error”.
    • Set test level to RunLocalTests.
    • Click “Next” and then “Deploy”.

Method 2: Using Salesforce CLI (Terminal-Only)

If you prefer not to use UIs or third-party sites, use the Salesforce CLI to deploy the same changes. Ensure you have the SF CLI installed and authenticated with your production org.

Command to Deploy

sf project deploy start --metadata-dir deployment.zip

This command deploys the deployment.zip containing your package.xml and destructiveChanges.xml.

Method 3: Using SFDX Git Delta

For aligning the deletion process with git commits, use the SFDX Git Delta plugin by Sebastien Colladon. This tool compares differences between two git commits and generates the necessary files for Metadata API deployment.

Step-by-Step Instructions

  1. Install the Plugin:

    sf plugins install sfdx-git-delta
    
  2. Delete Files and Commit Changes: Start in your Salesforce project, delete the files, and commit the changes.

  3. Generate Output Files:

    sf sgd source delta --to "HEAD" --from "HEAD~1" --output "."
    
  4. Deploy the Changes:

    sf project deploy start -x destructiveChanges/package.xml --post-destructive-changes destructiveChanges/destructiveChanges.xml
    

Conclusion

Summary Table of Methods

MethodTools RequiredSteps
Workbench (UI-Only)Workbench, Zip toolCreate XML files, zip them, upload and deploy via Workbench
Salesforce CLISF CLICreate XML files, zip them, deploy using SF CLI
SFDX Git DeltaSFDX Git Delta Plugin, SF CLIInstall plugin, delete and commit files, generate and deploy

Key Points

  • Deleting old Apex Code is crucial for maintaining Salesforce instances.
  • Teams often fail to delete code due to lack of native Salesforce UI and reliance on terminal-based approaches.
  • Learning these methods can simplify the task and offer benefits like removing conflicting code, reducing unused code, and raising test coverage.

Learn how to reduce technical debt by deleting code with one of these methods:

  • UI-only with Workbench
  • Salesforce-only tooling using the SF CLI
  • Automatically generate deletions based on git commits using the SFDX Git Delta Plugin by Sebastien Colladon (My favorite)

Need Our Help To Get Your Data Into Salesforce?

Join dozens of other companies by learning how you can get all your company's data in one place.

Back to Blog