56

I was recently told ( Deleting a Class with IDE - cannot delete from Production? ):

You cannot delete a class in production directly. You will need delete the class from your sandbox and then deploy the deletions to your production org. When you deploy from sandbox to production, the missing classes will come up in red and you can choose to deploy these deletions to Production.

However, I'd like some clarification because finding information on how to do this is like pulling teeth, unfortunately. Am I supposed to:

  1. Delete the Trigger in Sandbox
  2. Make a deployment package in Sandbox
  3. Add the Trigger which is going to be listed in RED now
  4. Deploy (and that deletes it from Production)?

I am not seeing deleted Triggers in Red in Sandbox. Are they NOT listed under "Triggers" for deployment, but elsewhere? Frustrated = true. Does this ONLY apply to Classes??

UPDATE: I just tried again, with a CLASS, and it still doesn't show up at all as a Deployable package (red or not) after I delete it in the Sandbox.

UPDATE 2: I was just able to delete a class, that I know was written via IDE from a third party, in both Sandbox and Production. So now I'm wondering if it has to do with where it was originally deployed from (if from IDE, IDE can delete it - it from Sandbox, you have to jump through hoops). :-(

6 Answers 6

69

An easy way to do this is as follows:

  1. Use an IDE to create a new project and download all classes from production.
  2. In the class you want to delete, open the metadata file and change the status of the class to "deleted".
  3. Save to the server.

Observe the status of the class in the example below:

     <?xml version="1.0" encoding="UTF-8"?>
     <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
         <apiVersion>23.0</apiVersion>
         <status>Deleted</status>
     </ApexClass>

You should try this in sandbox and become comfortable with it before attempting it in production. Double-check the class name before deleting.

Salesforce Documentation

7
  • I shall try this. Good Lord, I hope it's this simple...
    – AMM
    Dec 28, 2012 at 15:56
  • 2
    This works flawlessly. Thanks so much. I can finally get rid of about 10 classes and 20 triggers that have previously only been "commented out". :)
    – AMM
    Dec 28, 2012 at 16:11
  • 1
    Mohith, you're the man.
    – JannieT
    Nov 26, 2013 at 16:39
  • 1
    @PaulFischer This may be due to reason that there is some dependency of the class and thats not letting you to delete the same .Try deleting in sandbox to undertand dependency May 12, 2014 at 16:16
  • 1
    BTW, a simple way to delete multiple classes/triggers, is to uncheck Build Automatically from the Project menu. Change the status to Deleted and save for each. Then, Highlight the metadata file for each (Ctl-Click), right-click and Save to Server. It will run all Test Classes, but only once instead of once for each trigger/class. Mar 2, 2016 at 23:45
15

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.

3
  • NOTE: make sure to ZIP the two files and not the folder they are inside or else it will fail
    – Dieskim
    Oct 29, 2019 at 3:45
  • This solution is missing the folder of the metadata of the specific files to be deleted. Still need to zip the folder with apex classes of "Deleted" status, destructiveChanges.xml & package.xml
    – compski
    Jul 29, 2020 at 12:29
  • NOTE for triggers this <name>ApexClass</name> needs to be <name>ApexTrigger</name>, so it says ApexTrigger instead of ApexClass
    – Bahman.A
    Jan 5, 2021 at 19:45
14

If you are using VS Code you can remove the classes and triggers using Terminal command.

  1. Find the class/trigger that you want to delete and change the metadata for status to be Deleted.

  2. You will need to specify the name of the class/trigger in the sfdx force deploy command, and also you can delete multiple class/triggers per command (you will separate them with ,)

Here is example of the command:

sfdx force:source:deploy -m ApexClass:name_of_the_class,ApexClass:name_of_the_class2,ApexTrigger:name_of_the_trigger -l RunLocalTests -u AliasOfTheProject
5
  • 1
    Thanks for this, it was a lifesaver! Feb 25, 2020 at 21:22
  • Im glad I was able to help :)
    – Brane
    Feb 25, 2020 at 22:02
  • 1
    Same here - this helped me tremendously! Mar 29, 2020 at 13:53
  • For some reason it doesn't work for me, "sfdx force:source:deploy -m ApexClass:CBTN_Field_History_Tracker -l RunLocalTests" when I execute this I get this "=== Deployed Source No results found". And the class is still there in the org.
    – Bahman.A
    Nov 21, 2020 at 0:08
  • Its probably something with the settings in your VS Code... can you try creating new Project, retrieve everything from SF and then trying to push it?
    – Brane
    Nov 21, 2020 at 10:14
2

The way I've deleted triggers/classes from production is to delete the trigger/class from the sandbox and then deploy the triggers/classes folder. When you do that, eclipse will present you with the diffs between the source (sandbox) and target (production) and the deleted classes/triggers will be displayed there.

You can't deploy a deleted trigger/class in isolation, as it doesn't exist in the sandbox and thus there is nothing to deploy.

1
  • I'd like to add to this, but I had issues when I did this when deleting from Eclipse because I also told Eclipse to delete from the server as well as locally. When you delete from Eclipse locally, you don't want to actually delete from server in the Sandbox when you do that. If you do, it will actually delete the metadata that refers to it also locally and then doesn't allow you to deploy it.
    – dphil
    Apr 22, 2014 at 14:38
0

Since the question is asking about Trigger/Classes and I don't see any specific example of deleting a Trigger, I'm putting an example here. So follow the same approach that @sfdc has in his answer. I'm just putting an example of the destructive change XML file here. Note the difference here is in the tag where for classes we have ApexClass and for triggers we have ApexTrigger. You can have both in the same file but I wouldn't recommend putting both triggers and classes in the same destructive change file especially if there are dependencies between the triggers and classes.

<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>A_Trigger</members>
        <members>Another_Trigger</members>
        <name>ApexTrigger</name>
        
        <members>A_Class</members>
        <members>Another_Class</members>
        <name>ApexClass</name>
    </types>
    <version>30.0</version>
</Package>
-4

Using Eclipse, from a Sandbox project:

  1. Make a list of components to delete.
  2. Select all Classes, Triggers, Pages, or other Metadata components to delete.
  3. Right click on one of the selected components and click Delete in the menu.
  4. When prompted, click Yes to delete the component from the Server (Salesforce Sandbox). This will result in the components being removed from the Sandbox and from your local project in eclipse.
  5. Once the delete finishes, right click on the "src" directory in the project and click Force.com > Deploy to Server in the menu.
  6. Enter the credentials for your destination organization (another Sandbox, Developer Edition, or Production) and click the Next button until you get to the Deployment Plan part.
  7. IMPORTANT: Click the Deselect All button.
  8. Scroll until you reach the Delete action section.
  9. Check the box next to all components to delete (in your list).
  10. Click Next to deploy.

I recommend deploying the delete to another sandbox to confirm that everything works well before deploying to production. If done correctly, this process will delete all selected components from the destination organization.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .