A Tip A Day #24 – 3 Ways to delete an Apex class from Production

This post is a part of the daily blog series 

A Tip A Day, daily dosage of learning!


Day #24 – THREE Ways to remove an Apex class from Production

In a scenario, you no longer want to use the functionality in your apex class remove it from your Salesforce production org, so as to reduce the apex code usage, follow one of these 3 options.  Unlike in sandbox, where you can directly delete an apex class, you cannot do the same in a Production instance.  Its not that straightforward.

Note:  Many a times, you’d face a code coverage (less than 75%) issue while removing the class in production.  Just make sure, your overall code coverage is well above 75%.  Specially, if the apex class that you are removing has lots of code, it will definitely impact your overall code coverage.  So, be ready with your test classes for the other existing apex classes/triggers.

Important: Every time, you remove the apex class from production, make sure to compile all classes.  You can do this by going to Setup –> Apex Classes –> Compile All Classes.


Read my other post – 5 Ways To Stop Trigger In Production


Here are your 3 options.  You can choose whatever is easier for you, there’s no recommendation here.

1. USING ECLIPSE IDE

1. You need to install Force.com IDE if you don’t have already
2. Connect to the Sandbox Instance using the IDE and identify the apex class that you want to delete.
3. Open the trigger’s .xml file, and change the Status XML tag from Active to Deleted.
4. Save the file.
5. Select the two files (Code and XML) using “Ctrl-click,” and then right-click on one of them.
6. Select Force.com | Deploy to server.
7. Provide your credentials for the Production org and follow the steps.

2. THROUGH ANT TOOL

Here’s a way to do using ANT tool.

1. Initializing destructiveChanges.xml: First, create an empty destructiveChanges.xml file. Yours might start out looking like this:ISV Team Dev Package.xml 1.png2. Check your destructiveChanges.xml file in to your source code control system.

3. Propagating Destructive Changes to Other Developer Edition Orgs.  Every time you delete a component:

a. Identify the name of the metadata component member.
b. Identify the name of the metadata component type.
c. Add an entry to your destructiveChanges.xml file using this format:ISV Team Dev Package.xml 2.png

4. Repeat steps 3-a,b,c for every component you have deleted.
5. Check in the changes you made to your destructiveChanges.xml file to your source code control system.
6. Note that you should refresh the destructiveChanges.xml files and run the Force.com Migration Tool for Apache Ant against it.

3. COMMENT APEX CODE

Well this approach won’t delete the apex class.  Just comment your entire apex class code in your sandbox and then use outbound change set to deploy to production.  To comment code, you need to use /* and */ .  Anything in between those will be commented.

Public class BookController{
   /*
     your entire code
   */
}
Make sure there are no dependancies of other classes on this class.
 If you are looking to delete a trigger production, read my post about it – 5 Ways To Stop Trigger In Production

 


Read all other tips of the blog series here – A Tip A Day, daily dosage of learning!


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Advertisement

2 Replies to “A Tip A Day #24 – 3 Ways to delete an Apex class from Production”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: