Can any one tell me the steps to delete a class using destructiveChanges.xml
-
5Umm. There's no such thing as destructive change set? You mean "destructivechanges.xml"? Because regular changesets can never be destructive.– eyescreamCommented Nov 5, 2012 at 13:30
-
@Priyanka, I recommend selecting one of the answers as the best answer. IMHO it'd be sfdc's answer since it's the easiest, fastest, and still the best way to do such a thing today. Thanks!– Bahman.ACommented Apr 12, 2021 at 16:52
5 Answers
- Fulfill the PreRequisites for using the Force.com Migration tool
- Install the Java JRE/ JDK
- Install Apache Ant
- Copy the
ant-salesforce.jar
file from the unzipped file into the ant lib directory. - Follow the steps at Apex deploying using Ant to understand the basic setup
- Construct your
destructiveChanges.xml
file using Propagating Destructive Changes
Sample:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>codepkg</fullName>
<types>
<members>SampleDeployClass</members>
<members>SampleFailingTestClass</members>
<name>ApexClass</name>
</types>
<version>26.0</version>
</Package>
Run the Force Migration tool
C:\>ant undeployCode
Here's a Video Tutorial
From this blog
To achieve this via Workbench, create a folder on your desktop. I will call my folder ‘deleteClasses’.
Then go to Notepad (or another text editor) and copy and paste the below and save as the file with ‘package.xml’ and ‘All files (.)’.
<?xml version="1.0" encoding="UTF-8"?> <Package xmlns="http://soap.sforce.com/2006/04/metadata"> <version>30.0</version> </Package>
- Then create a new file in Notepad (or another text editor) and copy the below into it:
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://soap.sforce.com/2006/04/metadata"> <types> <members>SomeClass</members> <name>ApexClass</name> </types> <version>30.0</version> </Package>
Replace SomeClass with the name of your class that you wish to delete. If you have two classes that need to be deleted at the same time, you can simply add another row into the file with the name of the other class, for example:
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://soap.sforce.com/2006/04/metadata"> <types> <members>SomeClass</members> <members>SomeOtherClass</members> <name>ApexClass</name> </types> <version>30.0</version> </Package>
Save this file name as destructiveChanges.xml (note the capital ‘C’ in ‘changes’). Make sure the file is saved as ‘All files (.)’. More information on destructive changes can be found here.
Now there are two files in your folder. Open the folder, select both the XML files, right-click and select ‘Send To > Compressed Folder’. Keeping the default name of ‘package’ for the folder is fine.
You are now setup to deploy the destructiveChanges.xml file to Salesforce. To do this, go to Workbench and login with your credentials. It is recommended that you login to a Sandbox instance before you deploy the file to production.
Select Migration > Deploy.
Click ‘Browse’ and select the .zip package file. Then check ‘Rollback on Error’, ‘Single Package’, and select Test Level with ‘RunLocalTests’. More information on the Test Level can be found here.
Finally, select ‘Next’ and then you will notice that the success or error results will be displayed in Workbench once the deployment process has been completed.
This is a very easy way to delete Apex classes and can be very handy if you need a lightweight tool to do the job.
-
amen, thanks for posting this. this approach is way more lightweight unless you are already using ant or eclipse on a daily basis.– goravCommented Apr 25, 2017 at 16:47
Using force.com migration tool with ANT allows you to deploy destructivechanges.xml. http://www.salesforce.com/us/developer/docs/daas/index.htm
The sample package provided includes a destructive changes example.
Advantages of using this over doing it straight from Eclipse are:
- You can replicate the operation against different sandboxes /
production by running the script again. So for example you can first
test that the destructive deploy works as you want in a UAT
environment and then run the script connecting to production.
- You don't tie up your eclipse waiting for a response (for a big org a production deploy takes significant time)
The fastest way to do this is by adding your Production org in Force.com IDE / Eclipse and delete the class via the IDE. You will have to make sure that no dependencies exist before deleting, else the delete will fail.
Remove the dependencies by deleting the class first in your sandbox, then seeing why the delete would fail, do those changes, deploy it to your production org, then delete the class via Eclipse
Edit: You could also use the Force.com Migration Tool with Apache Ant to make desctructive changes, however this takes a little coding and installing ant and generally takes a little longer to set up compared to the eclipse method if you only need to do this a few times. more about that here: http://wiki.developerforce.com/page/Propagating_Destructive_Changes
-
As far as I know, simply deleting a class on production would only work on sandbox environments? In any case, just tried this through both the Force.com IDE and the developer console but that doesn't work. For completeness, I did verify that the class doesn't have dependencies.– KoenVMCommented Jun 21, 2017 at 9:01
Or just run the sfdx force:source:delete in your prod org :)
e.g.
sfdx force:source:delete --sourcepath force-app/main/default/classes/classToDelete.cls --noprompt -l RunLocalTests -u PROD