Developers

An Easier Way to Delete Apex Classes From Production

By Andy Hitchings

When attempting to delete Apex classes & triggers from production, you can be faced with a number of issues. This is due to the fact you cannot modify Apex code directly in production.

Within this article, I will go through a lightweight, and flexible solution, that enables you to delete Apex classes and triggers from production, quickly, and easily.

The Problem

The most common approach to deleting Apex classes and triggers in a Salesforce production environment is to leverage either the Force.com IDE or the Force.com Migration tool. These tools have a number of downsides, namely:

  • The Force.com IDE is very ‘heavyweight’ and is known for being quite buggy sometimes and unpleasant to use.
  • They have a number of dependencies (a compatible version of Java, different IDE version etc).
  • Connectivity to Salesforce via the Force.com IDE may be an issue.
  • The Force.com migration tool (ANT) can take some time to learn how to use properly.

Apex classes and triggers cannot be deleted from a Salesforce production declaratively, unlike in Sandbox orgs. This is because security for Apex in Salesforce Production orgs is greatly increased. So how can one delete Apex classes and triggers from a production org without having to go through the inconvenience and time consuming effort of installing, testing, and learning how to properly use these tools?

The answer can be to use Notepad Text Editor and the super lightweight and easy to use Workbench suite. Using these tools, deleting Apex classes and triggers from Salesforce production is a breeze. Because Workbench is web-based and text editors are already pre-installed and super easy to use, there’s no time spent on downloading, installing or testing the Force.com IDE or Force.com Migration tool (ANT).

The Solution

Let’s say that you have a Salesforce production org that has two Apex classes that need to be deleted.

  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>
Apex1

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

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

Apex2

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

Apex3

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

7.  Select Migration > Deploy.

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

Apex4

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

Summary

Deleting Apex classes and triggers can be tricky, but it certainly doesn’t have to be! There are tools at your disposal to help you out, and once you get the hang of it, you’ll be smooth sailing.

The Author

Andy Hitchings

Andy is a certified Salesforce and DocuSign admin and a certified Salesforce developer. His favorite technologies right now are the Force.com platform and Javascript.

Comments:

    Joey Chan
    April 29, 2016 6:19 am
    Here is another way to do it using IDE like Force.com IDE or MavensMate: 1. Force.com IDE should be installed. 2. Connect to the Sandbox Instance using the IDE and find the class or trigger that you want to delete. 3. Open the matching .xml file, and change the Status XML tag from Active to Deleted. 4. Or to disable the trigger change it to Inactive. Note: Apex class Status can only be changed to "Active" or "Deleted," not "Inactive". 5. Save the file. 6. Select the two files (Code and XML) using "Ctrl-click," and then right-click on one of them. 7. Select Force.com | Deploy to server. 8. Provide your credentials for the Production org and follow the steps. Source: https://help.salesforce.com/apex/HTViewSolution?id=000006188
    Nishant
    April 29, 2016 9:42 am
    I have come across very simplified tool Metaforce, it can be installed as plugin in chrome. User interface is very simplified for deleting metadata.
    Bonny Hinners
    April 29, 2016 4:43 pm
    I wrote about an easy way to delete classes and deactivate triggers using Workbench and setting status rather than destructive changes, which are more suited to removing customizations. You can find my step-by-step guide here: http://www.snugsfbay.com/2016/04/painless-removal-of-boat-anchors.html
    Madhavi
    May 12, 2016 2:27 am
    Very Helpful Article
    Vinod Kumar
    June 07, 2016 10:19 am
    Hi Joey.. I have tried above steps for UAT sandbox, after changing to Active state to "deleted" sate in meta data I can save the file, then next step I don't option to Deploy to server/save to server. Please help me here. Thanks Vinod
    Vinod Kumar
    June 07, 2016 10:42 am
    Hi Joey.. I came across your points, at 7th point I'm getting only "Force.com > show in salesforce web " this option. Pls help me out, its urgent. Thanks Vinod Kumar
    Josh
    October 31, 2016 5:45 pm
    Hey we got to the last step and then it failed. Screenshot here - http://imgur.com/a/BpsOb We followed the steps exactly. Where should I start debugging?
    Matt
    January 21, 2017 2:46 am
    Thank you, Thank You, Thank You ..... worked like a charm.
    Yidan
    April 03, 2017 5:28 pm
    Thank you for this helpful tutorial. I'm trying to delete a trigger. I've uploaded the zip file but the deployment failed because of some failed tests. Any clue how to bypass that and delete the trigger anyway?
    SriKrishna
    May 10, 2017 1:52 pm
    Hi Sir, I have tried to delete the classes from my Sandbox by following the steps as mentioned above (by using Workbench tool). Now how can I delete the same classes from my Production org ? Please assist me.
    Marques Hollie
    May 29, 2017 2:37 am
    Thank you so much for this! I needed to delete two apex classes and was struggling mightily with the Force. come IDE but this worked like a dream.
    Nishant
    June 17, 2017 3:51 am
    Man!!!! You are a true genius. I just found this after spending enormous unsuccessful hours in IDE. This is a life saver. I successfully deleted classes from Production without any issue. Thank you so much!!!!!
    non lag ma
    June 30, 2017 2:14 am
    I wish to convey my respect for your kind-heartedness for folks that have the need for guidance on the question. Your special commitment to getting the message all-around ended up being unbelievably valuable and have in most cases made workers like me to reach their dreams. This warm and helpful guideline indicates so much to me and extremely more to my mates. Best wishes; from each one of us.
    Kim
    July 19, 2017 7:47 pm
    Love this article. Quick question, when deleting apex classes and triggers in production do you have to run the tests at all? Can you choose to not run any tests?
    David Berman
    August 31, 2017 1:22 am
    Kim, tests have to be run in Production, but if you have test classes directed directly at the apex class to be deleted, you can specify to only run that test class.
    David Berman
    August 31, 2017 1:23 am
    Yidan, you will need to update your test classes, so that they don't fail before you can delete classes.
    Kim
    October 27, 2017 1:58 pm
    Thanks David, So I have a scenario where we have to delete apex classes, triggers, and test classes. They all depend on each other. When I opt to run local tests it fails but when I choose not to run tests at all it's successful. Can I choose not to run local tests when deploying to prod ?
    Colin
    October 30, 2017 3:44 pm
    When deleting triggers the destructiveChanges.xml file needs to contain SomeTrigger SomeOtherTrigger ApexTrigger
    Colin
    October 30, 2017 3:49 pm
    When deleting triggers the destructiveChanges.xml file needs to contain <members>SomeTrigger</members> <members>SomeOtherTrigger</members> <name>ApexTrigger</name>
    Troy
    November 23, 2017 1:38 am
    Pure gold! Have been trying to delete old/unused classes and triggers without luck. This guide worked perfectly. Definitely recommend running in a test environment to ensure all goes well; and then altering issues as they arise. Thanks!
    Della Street
    January 22, 2018 6:22 pm
    You saved my life. Thanks
    Matt Holloway
    January 25, 2018 9:32 pm
    I have used these steps before with no problem, but when trying to use this today, I am getting a "Bad file:Content is not allowed in prolog." error when it is trying to perform the delete through workbench. Anyone know how to correct? The only difference in my files from what is specified in the blog is the Class Name.
    Matt Holloway
    January 30, 2018 9:56 pm
    So, to close a loop here. It appears that this no longer works on Mac (not sure why) but using the same files and everything it worked just fine on a PC. At least I have a workaround!
    Michele Kleinhomer
    January 31, 2018 1:42 am
    This is the best utility ever! Thank you!!!
    Louisa Barrett
    February 20, 2018 8:13 am
    Hi, I've created my package.zip for a trigger and a class that I would like deleted. When I run it in the Workbench, I'm not getting any errors, but the trigger or the class are not being deleted. In the tree that appears below the results section on Workbench, Success = true, but Deleted = false Can anyone offer any suggestions?
    Nick
    April 17, 2018 6:57 pm
    I'm tryin gto delete my test class, but it fails too.
    Nick
    April 17, 2018 7:20 pm
    Thanks Joey. Used Maven's Mate to delete an Apex Class.
    Nick
    April 17, 2018 8:01 pm
    Actually solved it, by using workbench, I didn't select RunLocalTest but selected RunSpecifiedTest from the Test Level drop down, then chose the test class that I was deleting.
    Jessica
    May 15, 2018 7:22 pm
    I'm trying this on my macbook and there is no all files option when saving it from textedit app but there is a file type Word 2003 XML Document (XML) so I used that because I assume the file just needs to be XML. However I got this error from the workbench. Anyone know what this means: Bad file:Element {urn:schemas-microsoft-com:office:office}DocumentProperties invalid at this location in type Package Thanks
    andrea
    May 16, 2018 2:46 pm
    Usually I never comment any post but you saved me a whole lot of time with this one! I spent a frew hours with Eclipse trying to delete one class but I was unsuccessful... this was simple, fast and effective! Thank you!
    Penelope Gatlin
    June 06, 2018 10:37 pm
    Thanks that worked for me too
    Sandeep
    July 13, 2018 1:35 pm
    Hi Is there any way to Delete a class or Trigger in Production with running Specified tests("Run specifed Tests") and not running all the Local tests.
    John
    August 12, 2018 2:32 am
    Sandeep, try clickdeploy.io. You can specify tests with destructive deploy quite easily. I discovered the tool after spending too much time with ants and change sets. I am using their free tier at the moment and just love it.
    Kerry
    August 12, 2018 2:59 am
    I tried all options. Workbench seems to work best.
    Lavanya
    April 15, 2019 9:33 pm
    What is the use of Pacakge.xml in here?
    Julie
    August 06, 2019 7:53 pm
    Having the same issue as Jessica. Would love to know what the solution is for this.
    Carter
    August 29, 2019 10:13 pm
    Oh My GOD! What were those developers THINKING? This is A MESS! Doesn't salesforce see it for themselves these are no enterprise-worthy solution? A PROD environment should NEVER tell the administrator what not and what not to do. A better approach would be to allow ALL actions for admins but simply store deleted classes somewhere so in case of error they can be put back at the click of a button. Not all this bloody mess. I get upset about it honestly. It's a lazy company's solution.
    Carlos
    October 25, 2019 3:33 pm
    This was very helpful! Thank you Andy! quick and easy way to delete classes!
    Nicolle Bennett
    November 25, 2019 4:40 pm
    This is awesome, thank you!!
    Patrick Murphy
    December 14, 2019 6:00 am
    Outstanding. Thank you!
    Michele
    January 31, 2020 4:21 pm
    Just ran into this as well. The mac text editor adds formatting info to the XML file. I downloaded another plain text editor then opened my XML and the code was unrecognizable. There is also a problem with the mac zip files, stuff is added and Workbench will say "package.xml not found". I found an article to work around that and Workbench said it deployed successfully but the Apex classes were still there. I gave up on using the mac and used my personal laptop at home.
    jason
    April 03, 2020 8:12 pm
    This is awesome. Took 5 min to delete a trigger. I plan on setting up a new dev environment now that Mavensmate is no longer available but that will take me a while to build out and really needed something quick and painless.
    Naty A.
    May 29, 2020 10:26 am
    Amazing solution! Easy and direct to the point. Thank you for sharing :)
    John Tracey
    July 10, 2020 1:52 pm
    Thank you, Thank you, Thank You!!! We were stuck trying to uninstall Rootstock and Financial Force from one of our clients' org. This solution worked perfectly for out problem.
    Mayra Rock
    September 22, 2020 8:23 pm
    What would the name be for a Visualforce page? Would is be ApexPage? I realize I can manually delete a Visualforce page in PROD. However, I have to deploy the deletion of the page with the deletion of the controller and test class because they reference the page.
    eno
    January 12, 2021 10:35 pm
    So i tried something like this....and funnily enough it doesnt work. That Licence_Allocation_Transaction_Test test class, for some reason runs...even though i'm deleting it....w Licence_Allocation_Transaction ApexTrigger Licence_Allocation_Handler TestDataUtility_LicenseAlloc Licence_Allocation_Transaction_Test ApexClass
    swapnadip
    March 10, 2021 4:39 pm
    If you are on Mac and getting an error - No Package.xml found, read here to know how to fix it https://swapnadip.medium.com/delete-apex-classes-triggers-from-salesforce-production-bea7ebee17aa
    Lucy Mazalon
    March 11, 2021 10:44 am
    Thanks for sharing the additional resource.
    Tim
    March 18, 2021 11:54 pm
    I still cannot get it to work, It runs but the classes listed as members are not deleted.
    Laurel
    May 21, 2021 10:27 pm
    I'm so close.... this was SO much better than anything else I found. Very clear steps with screenshots. Thank you! I'm trying to delete an apex class. I was able to do it in the sandbox but it fails when I try it this way. Any suggestions?
    Mike R
    September 14, 2021 8:02 pm
    Fantastic article! I have a lengthy process to delete an unmanaged application which involves several apex classes & their test classes. In my sandbox, using Workbench, I'm doing exactly as the article suggests except for "Test Level", I'm selecting "RunSpecifiedTests" and passing my test classes (which are also included in my destructiveChanges.xml file). I get a successful run however I notice that it doesn't actually run the tests (I've monitored the Workbench UI & the Deployment Status in SF) although every class is successfully deleted. My main concern is once I get ready to run this in Production, that I'll get an error. Has anyone noticed what I have above and has successfully removed from Production? Thanks in advance!
    Jason_Spencer
    February 28, 2022 5:13 pm
    The run specified test nailed it for me Nick--Thank you
    Fredrik Burlin
    April 16, 2022 2:01 am
    Or just run the following in your prod org sfdx force:source:delete --sourcepath force-app/main/default/classes/classToDelete.cls --noprompt -l RunLocalTests -u PROD
    jason rojas
    May 04, 2022 7:16 pm
    you saved my life today and my sanity with one article. thank you
    Weida
    July 18, 2022 4:31 pm
    Fantastic article.! Thank you so much, Andy. The steps are so easy to follow. We have a lot of obsolete apex classes/triggers which we had no way to remove them. Finally, we are on our way to do it!
    James Tran
    August 17, 2022 6:55 pm
    Very useful article Andy! Thank you very much. On the final step I found that using RunSpecifiedTest instead of RunlocalTests option is faster for me as I could just use a simple apex test class to speed up the process.
    Ellen M Lichtenberger
    October 06, 2022 4:06 pm
    Worked like a charm - thank you!
    Megan James
    May 03, 2023 11:55 pm
    Does anyone know if this only works on PCs? I have been working with Salesforce Support and they can get past the tests on their PC but I cannot on my mac. I keep getting this error: problem: Bad file:Element {urn:schemas-microsoft-com:office:office}DocumentProperties invalid at this location in type Package
    Greg Diamond
    June 01, 2023 4:53 am
    So extremely helpful! I had been struggling with these Salesforce limitations all day. To answer a few of the comments and reiterate what James Tran says above, I found when I used RunLocalTests it took longer and ran into some unrelated code coverage issues. However, because you are deleting, you don't actually need code coverage for anything, so pick your favorite test class that has more than 75% coverage and use that with RunSpecifiedTest. It doesn't seem to matter that it is unrelated, just needs to pass 1 test to show you ran something.
    soma
    July 07, 2023 2:19 pm
    Do I need to add test classes while deleting the classes , I didnt add any test classes and deleted around 100 classes in Dev and UAT by using your suggestion. thanks

Leave a Reply