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
-
Go to your desired folder in your system and right click to create a new file.
-
Choose New and select Text Document and name it destructiveChanges.xml
-
Create another file with the name package.xml
2. ADD COMPONENT NAMES TO THE XML FILES
-
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>
-
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.
-
You can add as many name and member tags as you want if you want to delete more metadata types.
-
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>
-
Remember the API version mentioned in the files and make sure both are the same.
3. GENERATE A ZIP FILE
- Zip these files destructiveChanges.xml and package.xml into combined.zip
4. OPEN WORKBENCH AND DEPLOY
- 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.
- Enter the credentials of your org, login and click allow if prompted.
- Select migration tab and click deploy.
- Choose the created zip file from your system.
-
Check the options Rollback on error, Single package and for test level select Run All Tests or Run Local Tests based on your requirement.
-
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.
-
Click Deploy and when the tests are run, you will see the Results and whether the deployment is successful or not.
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
-
Open VS code and click Ctrl + shift + P in windows or Cmd + shift + P in mac to open the command palette.
-
Search for SFDX: Create Project with Manifest and select it.
-
Select Standard Project template.
-
Type the project name and hit Enter.
-
Select a folder in your system to save the project files.
2. AUTHORIZE THE ORG
-
Open command palette and search for SFDX: Authorize an org and select it.
-
Select Production, type any Org alias and hit Enter.
-
You will reach the browser page of Salesforce for logging in. Enter the credentials and click allow.
-
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
-
Install the extension Salesforce Package.xml Generator for VS Code
-
Open the command palette and select the command SFDX Package.xml Generator: Choose Metadata Components.
-
Select the required Apex classes, Triggers and click Update Package.xml
-
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.
4. DELETE APEX CLASSES OR TRIGGERS
-
Expand the force-app and you will see the classes and triggers from the org.
-
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.
-
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.
-
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