back to resources
Deleting an Apex Class or Trigger from Production
August 12 2021 • 25 min read

BUSINESS CHALLENGE

Apex classes and triggers can be directly deleted in sandbox but cannot be deleted in Production.

This blog post will help you delete the Apex classes and make the triggers Inactive or delete in the production org.

The Apex classes or triggers in the production can be deleted in a few different ways. Here we are going to do it in two ways. One is a conventional way using the workbench and the other one, using the VS code.

USING VISUAL STUDIO CODE

  1. Create sfdx project with manifest

  2. Authorize the org

  3. Delete Apex classes or triggers

    1. CREATE SFDX PROJECT WITH MANIFEST

    1. Open VS code and click Ctrl + shift + P in windows or Cmd + shift + P in mac to open the command palette.

      screenshot 340

    2. Search for SFDX: Create Project with Manifest and select it.

    3. Select Standard Project template.

      screenshot 400

    4. Type the project name and hit Enter.

      screenshot 402

    5. Select a folder in your system to save the project files.

    2. AUTHORIZE THE ORG

    1. Open command palette and search for SFDX: Authorize an org and select it.

      screenshot 403

    2. Select Production, type any Org alias and hit Enter.

      screenshot 404

    3. You will reach the browser page of Salesforce for logging in. Enter the credentials and click allow.

      screenshot 459

    4. Go back to the VS code and you will see the notification SFDX: Authorize an Org successfully ran.

      screenshot 460

    3. DELETE APEX CLASSES OR TRIGGERS

    1. Expand the Manifest directory and right click on package.xml and select SFDX: Retrieve Source in Manifest from Org and you will see SFDX: Retrieve source from org successfully ran.

      screenshot 351

      screenshot 353

    2. Expand the force-app and you will see the classes and triggers from the org.

      screenshot 376

    3. Click on the xml file of the class or trigger that you want to delete from the org and change the value in the status tag to Deleted to delete an Apex class or trigger and Inactive for a trigger to make it inactive.

      screenshot 355

    4. save the file.

      screenshot 377

    5. Right click on the xml file that you saved and select SFDX: Deploy source to Org and you will see SFDX: Deploy Source to Org successfully ran for confirmation.

      screenshot 378

    6. Go back to your Org and you will see that the Apex class/Trigger is deleted.

USING WORKBENCH

  1. Create xml files

  2. Add code to the xml files

  3. Generate a zip file

  4. Open workbench and deploy

    1. CREATE XML FILES

    1. Anywhere in the file explorer create a folder and name it deleteApexclass.

    2. Create two Text documents with the names destructiveChanges.xml and package.xml.

      screenshot 380

    2. ADD CODE TO THE XML FILES

    1. Open the destructiveChanges.xml with the text editor tools ( like Notepad++) and paste the following code.

      <?xml version="1.0" encoding="UTF-8"?>
      <Package xmlns="http://soap.sforce.com/2006/04/metadata">
      <types>
      <members>TestClass</members>
      <name>ApexClass</name>
      </types>
      </Package>
    2. In the members tag add the name of the Apex class that you want to delete and in the name tag add the type i.e. whether it is an ApexClass or an ApexTrigger and save the file.

    3. Open the package.xml with the text editor, paste the following code and save it.

      <?xml version="1.0" encoding="UTF-8"?>
      <Package xmlns="http://soap.sforce.com/2006/04/metadata">
      <version>51.0</version>
      </Package>
    4. Remember the API version mentioned in the package.xml file.

    3. GENERATE A ZIP FILE

    1. Zip these files destructiveChanges.xml and package.xml into combined.zip.

      screenshot 379

    4. OPEN WORKBENCH AND DEPLOY

    1. Open Workbench and before logging in make sure that the version mentioned in the package.xml file and the selected API version of workbench are the same.

      screenshot 382

    2. Enter the credentials of your org, login and click allow.

    3. Select migration tab and click deploy.

      screenshot 384

    4. Choose the created zip file from your system.

      screenshot 385

    5. Check the options Rollback on error, Single package and for test level select Run All Tests.

      screenshot 389

    6. Click Next and you will see a warning that the deployment will make permanent changes to the org’s metadata and cannot be rolled back.

      screenshot 386

    7. Click Deploy and within a few seconds you will see that the success is true.

      screenshot 387

    NOTE

    • Multiple Apex classes can be deleted by adding their names, each in a member tag inside the types tag.

    • In order to delete a trigger along with the Apex class, just add another types tag and you can delete as many as you want by including them in members tag.

      screenshot 393

WRAPPING IT UP

In this post we have learned about how to delete the unwanted Apex classes or  triggers in production using VS Code and Workbench.

Leave a Comment

Your email address will not be published

© 2022 Digital Biz Tech