Best way to delete apex class in production? - Answers - Salesforce Trailblazer Community
Ask Search:
Will JohnstonWill Johnston 

Best way to delete apex class in production?

I've used the following method in the past to delete apex classes from production. Is there a better or easier way to do it?

https://www.salesforceben.com/way-to-delete-apex-classes-from-production/

(The title of the article is "An Easier Way to Delete Apex Classes from Production," but it's a few years old now, and I'm looking for an *even* easier way. I guess I'm greedy?)

I have found numerous old references to using the Force.com IDE but the IDE has been officially retired. I'm also looking for an option that doesn't involve the ANT Migration tool.
Best Answer chosen by Will Johnston
Will JohnstonWill Johnston
Amnon,

Thanks for the reply. The link you provided doesn't appear to work. However, I searched for ways to use the CLI to delete apex classes and stumbled upon this post which looks like you wrote. Is this what you were referring to? 

https://www.linkedin.com/pulse/how-delete-classes-triggers-from-salesforce-production-amnon-kruvi/

It was a good read, and the "Option 1" steps laid out using the CLI do look like a step in the right direction and a simpler approach. I will give this a try. 

I do think Salesforce should provide a simpler, and possibly declarative way, of deleting classes though. 

Thanks,
Will

All Answers

Amnon KruviAmnon Kruvi
Hi Will, 

Although you will still need to create the destructive changes XML file, you can now delete classes using the Salesforce SFDX CLI, which would be slightly easier. Have a read here: (http://us.api.meta/api/sforce_api_objects_flowinterview.htm)
Will JohnstonWill Johnston
Amnon,

Thanks for the reply. The link you provided doesn't appear to work. However, I searched for ways to use the CLI to delete apex classes and stumbled upon this post which looks like you wrote. Is this what you were referring to? 

https://www.linkedin.com/pulse/how-delete-classes-triggers-from-salesforce-production-amnon-kruvi/

It was a good read, and the "Option 1" steps laid out using the CLI do look like a step in the right direction and a simpler approach. I will give this a try. 

I do think Salesforce should provide a simpler, and possibly declarative way, of deleting classes though. 

Thanks,
Will
This was selected as the best answer
Amnon KruviAmnon Kruvi
Ahh sorry, that link was from a different answer that somehow made it into this one (don't ask me how).

There's actually a very good reason not to provide declarative options for deleting code modules. Code can be related to other code in lots of different ways, and the system doesn't necessarily "know" that a class is being used by something. It requires someone with a technical overview of the system to do that. We can't even touch classes directly on production orgs. 

But give it a try and I'll be here if you run into any trouble. 
Will JohnstonWill Johnston
Thanks. Using the CLI will work fine for me as a developer. But I could see an admin etc. wanting to do this as well. 

For the CLI, it looks like source:delete supports deleting from a sandbox, I wonder if it will support deleting from production in the future?

https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_source.htm

As for a declarative option, that's a good point about dependencies. I was thinking there would be checks similar to when you try to delete a field that has references to it. However, I just tried in a sandbox to delete one class that has one of its methods called by another class, and the deletion was allowed. I was expecting a dependency check to block it.

I would also think that a declarative option would work like a change set, deploying from one org to another, so that it could be fully tested before updating production.

I do think Salesforce could provide better support and documentation for deleting classes in production. It feels like there's always hoops to jump through to do it and that it's difficult to find the steps to follow at all. But it does look like things have slowly improved over the years, and that your proposal is a step in the right direction.
 
Amnon KruviAmnon Kruvi
The CLI can delete from production. If you go through the article, the idea was to try and simplify the process for an admin by using the batch files available there - you'd only need to prepare the XML file, then run two files (one for login, one to delete). I can't speak for Salesforce in regards to future features, you can use the idea exchange to make a recommendation, but I wouldn't bet on anything like this being added. 
Will JohnstonWill Johnston
Thanks Amnon. Using your process of creating destructiveChanges.xml and sfdx force:mdapi:deploy should work great for me.

I may submit an idea, but if I do, I won't get my hopes up. Thanks again!