How to Delete an Apex Class in Production Using Salesforce CLI (SFDX)

In Salesforce development, cleaning up unused or obsolete metadata — like old Apex classes or triggers — is crucial for keeping your org lean, maintainable, and secure. But did you know that standard tools like change sets don’t support deletions? That’s where Salesforce CLI + destructiveChanges.xml comes in!

Step-by-Step Guide

1. Create a destructiveChanges.xml file

This file tells Salesforce which components to delete.


2. Create an empty package.xml file

Even for destructive deployments, a file is required:

You can adjust the API version if needed.


3. Zip the files

Run:

Or use any zip tool you like. Make sure both files are at the root of the zip.


5. Deploy using Salesforce CLI

Execute:

  • → Replace with your target org alias or username

  • → Mandatory in production to ensure code coverage


Important considerations

Test coverage: Even destructive changes require tests to pass in production.

Validation first: You can run to validate before deploying:

Rollback plan: Always validate in sandbox first and ensure no dependencies will break.


Official Salesforce Reference

Deleting Metadata Components with Metadata API

To view or add a comment, sign in

Others also viewed

Explore topics