Let’s imagine a situation when some Apex classes are no longer used in production. In that case, Salesforce recommends removing those classes from production to keep it clean from anything unnecessary. We can do that in a number of ways but that guide is dedicated to the Ant Migration Tool. The Ant Migration Tool is a Java/Ant-based command-line utility for moving metadata between a local directory and a Salesforce Org. For more information on tool usage cases visit the site. This article is aimed to acquaint you with the basics of the Ant Migration Tool and build your first solution to delete custom Apex Class from Salesforce Org.
Before you install the Ant Migration Tool you need Java and Ant to be set up on your local machine. Then you can download the Ant Migration Tool from a Salesforce Org. The detailed guide is under the link.
2 Delete Apex Class from an Org
2.1 Create project
Enter the folder where the Ant Migration Tool was installed. You can see the ant-salesforce.jar file, a brief description in the Readme.html file, and the sample folder with build.properties and build.xml files. We will use those files as templates in our project but firstly we should create our own project. In the directory with ant-salesforce.jar create a new folder named ‘removeClass’ as per image:
You can switch to another directory, but you shouldn’t forget to change the path for ant-salesforce.jar in the build.xml file.
2.2 Enter connection information
In order to connect to the Salesforce Org, you should provide either username with password, active Salesforce session, or OAuth access token. For more information visit this site. In that guide, we will use a username and password. Copy the build.properties file from the sample folder to your project’s folder. Then open with a text editor, and populate sf.username and sf.password parameters with your credentials. Make sure to add a security token to your password, cause you are accessing Salesforce via the desktop client. If you don’t know how to reset your security token visit this link for clarification. Populate sf.serverurl parameter and comment other parameters. Save the file. It should look like this:
2.3 Create package.xml
In general, to delete components the same procedure as with deploying components must be used, but also include manifest file removing that’s named destructiveChanges.xml and list the components to delete in this manifest. The format of this manifest is the same as package.xml. And to deploy the destructive changes, you must also have a package.xml file that lists no components to deploy, includes the API version, and is in the same directory as destructiveChanges.xml. Let’s create our own. Move to your project’s folder location. Inside the removeClass folder create a new folder called codepkg where the manifest files will reside. Inside codepkg create destructiveChanges.xml file and populate it with the class definition you want to delete. It should look like this:
Then create an empty package.xml next to destructiveChanges.xml:
2.4 Create undeploy target
The build.xml file specifies a series of commands to be executed by Ant Migration Tool. Within the build.xml file are named targets that process a series of commands when you run Ant with a target name. This is the place where we create our own target to remove the class from Salesforce Org. Copy build.xml file from sample folder and paste into removeClass folder. Open this file in the text editor. Remove all targets except one with the name undeployCode. It will be our end target. Make sure you have a path to ant-salesforce.jar specified, deployRoot parameter is set to codepkg and target name is undeployCode:
Also make sure the rollbackOnError parameter of the deploy command is set to true or omitted, as it defaults to true. For looking to other parameters you may specify in deploy command
Open your command prompt (WIN + ‘R’) and navigate to the removeClass folder. Then run the command ant undeployCode. Expected result - BUILD SUCCESSFUL:
Ant Migration Tool is a powerful tool to manipulate metadata on Salesforce Org. Specific Apex class was deleted successfully from Production Org. If you faced any issue during performing this task, check this helpful article with resolutions and best practices.