back to blog
Delete Metadata Components from Production
August 12 2021 • 25 min read

USE CASE

Create an artifact which can be used to delete the metadata components in a sandbox. This tested artifact should be reused to perform the same delete operation in different sandboxes or production.

We cannot delete metadata components using change sets. We can manually delete all other metadata components except Apex Classes and Triggers from Production.

Manually deleting metadata components from a sandbox or production are error prone as testing is needed in each org.

The Metadata components in the production can be deleted in a few different ways. In this blog we are going to discuss two different ways of deleting components. One is a conventional way using the workbench and the other one, using the VS code.

Out of the two ways the better way is to create an artifact, use it in workbench and test it in one sandbox and reuse it in the other related orgs.

Using VS Code we can delete only Apex classes and Triggers.

USING WORKBENCH

Step 1 : Create xml files

Step 2 : Add component names to the xml files

Step 3 : Generate a zip file

Step 4 : Open workbench and deploy

1. CREATE XML FILES

  1. Go to your desired folder in your system and right click to create a new file.

  2. Choose New and select Text Document and name it destructiveChanges.xml

  3. Create another file with the name package.xml

    screenshot 380

2. ADD COMPONENT NAMES TO THE XML FILES

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

    <?xml version="1.0" encoding="UTF-8"?>
    <Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
            <members>NameOfApexClass</members>
            <members>SampleClass</members>
            <name>ApexClass</name>
        </types>
        <types>
            <members>DisplayContacts</members>
            <members>DisplayAccountsAura</members>
            <name>AuraDefinitionBundle</name>
        </types>
        <types>
            <members>Opportunity_Record_Page</members>
            <name>FlexiPage</name>
        </types>
        <types>
             <members>sampleLwc</members>
             <members>recordEditFormLwc</members>
             <name>LightningComponentBundle</name>
        </types>
        <version>55.0</version>
    </Package>
  2. In the members tag add the API Name of the component that you want to delete and in the name tag add the API Name of the metadata type that you want to delete, and save the file.

  3. You can add as many name and member tags as you want if you want to delete more metadata types.

  4. Open the package.xml file and paste the following code and save it.

    <?xml version="1.0" encoding="UTF-8"?>
    <Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <version>55.0</version>
    </Package>
  5. Remember the API version mentioned in the files and make sure both are the same.

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 xml files and the selected API version of workbench is the same.

screenshot 382

  1. Enter the credentials of your org, login and click allow if prompted.
  2. Select migration tab and click deploy.

screenshot 384

  1. Choose the created zip file from your system.

screenshot 385

  1. Check the options Rollback on error, Single package and for test level select Run All Tests or Run Local Tests based on your requirement.

    screenshot 389

  2. 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

  3. Click Deploy and when the tests are run, you will see the Results and whether the deployment is successful or not.

    screenshot 387

USING VISUAL STUDIO CODE

Step 1 : Create sfdx project with manifest

Step 2 : Authorize the Org

Step 3 : Retrieve Apex classes and Triggers from the Org

Step 4 : 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.

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

    screenshot 403

  3.  Select Standard Project template.

    screenshot 400

  4. Type the project name and hit Enter.

  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.

    dcao17

  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.

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

3. RETRIEVE APEX CLASSES AND TRIGGERS FROM THE ORG

  1. Install the extension Salesforce Package.xml Generator for VS Code

    screenshot 497

  2. Open the command palette and select the command SFDX Package.xml Generator: Choose Metadata Components.

    screenshot 499

  3. Select the required Apex classes, Triggers and click Update Package.xml

  4. Right click on the package.xml file and select SFDX: Retrieve Source in Manifest from Org and you will see SFDX: Retrieve source from org successfully ran.

    screenshot 351

4. DELETE APEX CLASSES OR TRIGGERS

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

    screenshot 376

  2. 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. Save the file.

    screenshot 377

  3. 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

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

WRAPPING IT UP

In this post we have learned about how to delete the Metadata components from production in an efficient way using Workbench.

Leave a Comment

Your email address will not be published

© 2024 Digital Biz Tech