10

Can any one tell me the steps to delete a class using destructiveChanges.xml

  • 5
    Umm. There's no such thing as destructive change set? You mean "destructivechanges.xml"? Because regular changesets can never be destructive. – eyescream Nov 5 '12 at 13:30
17
  1. Fulfill the PreRequisites for using the Force.com Migration tool
  2. Install the Java JRE/ JDK
  3. Install Apache Ant
  4. Copy the ant-salesforce.jar file from the unzipped file into the ant lib directory.
  5. Follow the steps at Apex deploying using Ant to understand the basic setup
  6. 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

  • Seems like link in 5. is no longer available. – Paul T. Rawkeen Sep 28 '18 at 12:20
8

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:

  1. 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.
  2. You don't tie up your eclipse waiting for a response (for a big org a production deploy takes significant time)
6

From this blog

  1. To achieve this via Workbench, create a folder on your desktop. I will call my folder ‘deleteClasses’.

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

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

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

  4. Select Migration > Deploy.

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

  6. 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. – gorav Apr 25 '17 at 16:47
4

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. – KoenVM Jun 21 '17 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.