Skip to main content

Command Palette

Search for a command to run...

How to delete an Apex Class from Production using VS Code?

Updated
2 min read
How to delete an Apex Class from Production using VS Code?

Which method to choose?

There are various methods, including the Force.com IDE, Workbench, and Ant Migration Tool, to remove Apex classes from Production.

This guide will demonstrate how to use the Visual Studio Code IDE to remove a class or trigger from the production.

1. Create a project with manifest

First, create a project on Visual Studio Code, that is connected to your production environment. After authorizing successfully, retrieve the manifest.xml file from the source. Make sure that manifest.xml contains ApexClass as a member. After retrieving, you will see all the classes under the folder /force-app/classes.

Now, go to the specific class you want to delete.

2. Edit className.cls-meta.xml File

Let's say you want to get rid of the class badApexClass.

image.png

Go to your .cls-meta.xml file and change the status from Active to Deleted

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

3. Run the following command using the terminal

sfdx force:source:deploy -m ApexClass:badApexClass -l RunLocalTests -u ProjectAliasName

4. Check the Output

You should see something similar to this on your terminal:

=== Deployed Source

 FULL NAME                   TYPE      PROJECT PATH                                                            
 ─────────────────────────── ───────── ─────────────────────────────────────────────────────────────────────── 
 badApexClass        ApexClass force-app/main/default/classes/badApexClass.cls                 
 badApexClass        ApexClass force-app/main/default/classes/badApexClass.cls-meta.xml        

=== Test Results Summary

Passing: 10
Failing: 0
Total: 10
Time: 12789
Deploy Succeeded.

Congratulations! You've successfully deleted that Apex class with minimal effort.

2.9K views