Pages

Thursday, December 22, 2016

Salesforce: delete Trigger and Apex Class in Production

In previous blog deactivate Trigger in Production, we share how to deactivate trigger in Production environment, also option to comment whole trigger or apex class. Comment the whole apex class or trigger should not cause any issue, but that's not ideal, personally I prefer just to delete it.

But, as in the blog earlier, there is no Delete button in Trigger and Apex class. So how to delete it? In this blog, I'll use Workbench to help, you can do the same using Ant migration tool.

In short, you need the same procedure as with deploying components, but also to include a delete manifest file 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 except that wildcards aren’t supported.

1. Prepare the files
package.xml - you can copy and file from below:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <version>38.0</version>
</Package>

destructiveChanges.xml - make sure 'C' in capital
sample file:
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
  <types>        
    <members>Class1</members>
    <members>Class2</members>
    <name>ApexClass</name>
  </types>
  <types>
    <members>Trigger1</members>
    <name>ApexTrigger</name>
  </types> 
<version>38.0</version>
</Package>

Put both files above in a folder, zip the files (without folder) and name it as package.zip. For the name tag in the file above, see Metadata Types.


2. Workbench
Login to Workbench, select Migration | Deploy, select the zip file and enable both 'Rollback On Error' and 'Single Package'.


Click Next button button to continue, then Deploy button to proceed.



Wait for deployment, normally will take less than 1 minute. Once finish, you will get the result -- done and check the status.




Reference:


6 comments:

  1. How to delete unused Apex Class from developer sandbox ?

    I wrote some incorrect class which I need to delete from developer env.

    please suggest

    ReplyDelete
  2. Works fine for me. Thank you so much!

    ReplyDelete
  3. Unfortunately I keep receiving the error that package.xml can not be found. I did not see any hidden files when zipping files. Any advice would be appreciated!

    ReplyDelete
  4. Beth, if you are using a mac, you need to create/edit the XML file a different way. I use the app Brackets to do it.

    ReplyDelete
  5. I was able to solve this. Please check blog for step by step procedure..
    https://www.forcetalks.com/blog/deleting-apex-classes-apex-triggers-from-production-using-workbench-salesforce-tutorial/

    ReplyDelete