Destructive changes deployment

From PranavNagrecha/AwesomeSalesforceSkills · View interactive version

Destructive changes deployment is an AI agent skill from the GitHub repository PranavNagrecha/AwesomeSalesforceSkills. Managing destructiveChanges.xml manifests for safe metadata deletion in Salesforce deployments. Use when deleting metadata components via Metadata API or sf CLI. Covers pre vs post destructive manifests, safe deletion sequencing, dependency handling. NOT for package.xml basics (use apex/metadata-api

How to use this skill

Skill file

Destructive Changes Deployment

This skill activates when a practitioner or agent needs to delete metadata components from a Salesforce org using Metadata API or the sf CLI. It covers all three manifest variants, dependency sequencing, components that cannot be deleted via API, and the correct CLI flags.


Before Starting

Gather this context before working on anything in this domain:


Questions to Ask Before Configuring

| Ask | Why it matters | What a good answer adds |

|---|---|---|

| Are the destructive manifests hand-written, or copied from an existing package.xml? | A copied manifest drags in a <version> element; the version belongs in the companion package.xml, which must sit in the same directory. | Produces a destructive manifest with explicit members and no version, plus a version-only package.xml (Gotcha 1). |

| Does any deletion depend on an update in the same deploy clearing a reference first, such as a class that still uses the object? | Plain destructiveChanges.xml deletions run before additions, so the reference is still there when the delete runs. | Puts that member in destructiveChangesPost.xml (Gotcha 2). |

| Is any component deleted and re-created under the same name in this release? | Plain destructiveChanges.xml and the Pre variant both delete before additions (api_meta.txt L4648); only Post deletes after. | Names the variant on purpose instead of relying on the plain file (Gotcha 2). |

| How will other branches, local projects and the CI pipeline learn that these files are gone? | A stale local file redeployed later brings the deleted component back. | Deletes the source files in the same PR and adds retrieve-before-deploy or source tracking (Gotcha 3). |

| Does any flow on the list have an active version in the target org? | A flow with an active version cannot be deleted through a deploy, and the error may name it by internal ID. | Adds a deactivation step before the destructive deploy (Gotcha 4). |

| Does the cleanup include record types or picklist values, global value sets included? | Picklist values are not manifest members and need Setup work; record types are documented as deletable for custom objects (api_meta.txt L44970), but in-use ones are reported to fail (UNVERIFIED (2026-10-19)). | Moves them to a manual runbook step with an owner and a date (Gotcha 5). |

| Is any object or component on the list used by an active Lightning page or its action override? | Items tied to an active Lightning page can't be deleted by destructiveChanges.xml. | Adds the Lightning App Builder deactivation as a pre-deploy step (Gotcha 6). |

A proper configuration orders every deletion and lists the manual steps before the deploy, so the release removes exactly what was planned and nothing comes back on the next deploy.


Core Concepts

The Three Manifest Variants

The Metadata API supports three placements for destructive operations within a deployment, each controlled by the filename. All three files share the same XML schema as a standard package.xml.

destructiveChangesPre.xml — Deletions are processed before the additions and updates in the accompanying package.xml. Use this when the component being deleted would conflict with a new component being deployed (e.g., renaming a field by deleting the old one and creating the new one, where the old name must be cleared first).

destructiveChanges.xml (plain name) — The platform processes deletions at the same time as additions. In practice this behaves like a pre-deployment deletion. This is the legacy format from the Ant Migration Tool era and remains valid.

destructiveChangesPost.xml — Deletions are processed after all additions and updates in package.xml have landed. This is the required variant when the component being deleted is still referenced by another component that is itself being updated in the same deployment. The new version of the referencing component must land first (clearing the reference) before the deleted component can be removed.

All three files must be included inside the same deployment package as a package.xml. When using sf CLI flags, an empty package.xml (specifying only the API version) may be used as the companion manifest.

Manifest XML Structure

Every destructive manifest follows the same schema as package.xml. The <version> element is omitted from destructive manifests — version lives only in package.xml. Each <types> block groups members by metadata type.

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>MyObject__c.OldField__c</members>
        <name>CustomField</name>
    </types>
    <types>
        <members>OldValidationRule</members>
        <name>ValidationRule</name>
    </types>
</Package>

Member names must use the same format as they appear in package.xml — for child metadata types like CustomField, the format is ParentObject.FieldName.

sf CLI Flags for Destructive Deployments

The sf project deploy start command (Salesforce CLI v2) exposes two flags:

Example using pre-destructive:

sf project deploy start \
  --manifest package.xml \
  --pre-destructive-changes destructiveChangesPre.xml \
  --target-org myOrgAlias

The --manifest flag is required even when the package.xml contains no components to add; in that case use an API-version-only manifest.

Components That Cannot Be Deleted via API

The following need manual work in Setup instead of, or before, a destructive manifest:

Attempting to delete these via a manifest results in a deployment error. The error message is not always obvious about the root cause.


Common Patterns

Pattern 1 — Delete a standalone component with no inbound references

When to use: Removing a custom object, Apex class, Lightning Web Component, or custom metadata type that has no active references in other deployed components.

How it works:

  1. Confirm zero inbound references using Setup dependency tooling or sf project retrieve start to inspect local source.
  2. Build destructiveChanges.xml listing the component.
  3. Create an empty companion package.xml containing only the API version.
  4. Deploy using sf project deploy start --manifest package.xml --pre-destructive-changes destructiveChanges.xml --target-org alias.

Why not the alternative: Deploying without an empty package.xml companion will cause a deployment error — the Metadata API requires a valid package manifest in the deployment container.

Pattern 2 — Delete a component that is still referenced (use Post variant)

When to use: Removing a custom field or class that is still referenced by another component, where the referencing component is also being updated in the same deployment to remove the reference.

How it works:

  1. Build the updated referencing component (e.g., updated Apex class with reference removed, updated validation rule formula, updated page layout).
  2. Include the updated component in package.xml.
  3. Place the deletion in destructiveChangesPost.xml.
  4. Deploy: the updated component lands first (clearing the reference), then the deletion fires.
sf project deploy start \
  --manifest package.xml \
  --post-destructive-changes destructiveChangesPost.xml \
  --target-org alias

Why not the alternative: Using destructiveChangesPre.xml or plain destructiveChanges.xml in this scenario causes the deletion to fire before the reference is cleared, producing a deployment failure.

Pattern 3 — Troubleshooting a failing destructive deployment

When to use: A destructive deployment is failing and the error is not immediately clear.

How it works:

  1. Read the deployment error message carefully. Common patterns: "Component in use by...", "Cannot delete...", "Component not found".
  2. "Component in use" — find the referencing component and either update it first (use post variant) or remove the reference in a prior deployment.
  3. "Cannot delete" on an active Flow (api_meta.txt L68041), an in-use Record Type, or a Picklist value — handle manually in Setup.
  4. "Component not found" — the API name or type is wrong in the manifest; re-check exact casing and type name.
  5. Run the checker script: python3 scripts/check_destructive.py --manifest-dir <path>.

Decision Guidance

| Situation | Recommended Approach | Reason |

|---|---|---|

| Deleting a component with no active references | destructiveChanges.xml or --pre-destructive-changes | Simplest case; no ordering concern |

| Deleting a component that is still referenced, and clearing the reference in the same deploy | destructiveChangesPost.xml via --post-destructive-changes | Reference must be removed before deletion fires |

| Deleting a component whose API name conflicts with a new component being added | destructiveChangesPre.xml via --pre-destructive-changes | Old component must be cleared before new one can land |

| Deleting a Picklist value or an active Flow version; a Record Type still in use | Manual Setup action (deactivate the flow first) | Active flow versions can't be deleted (api_meta.txt L68041); record-type behaviour is UNVERIFIED (2026-10-19) beyond the documented delete support (L44970) |

| Deleting many components across multiple types | Single manifest with multiple <types> blocks | Metadata API processes all members in one transaction |


Recommended Workflow

Step-by-step instructions for an AI agent or practitioner working on this task:

  1. Inventory components to delete — List every component by exact metadata type and API name. Confirm no wildcards are being used (they are not supported in destructive manifests).
  2. Check dependencies — For each component, determine whether any other active component references it. Use Setup dependency tooling, org-wide metadata retrieval, or sf project retrieve start to inspect locally.
  3. Choose the correct manifest variant — Use destructiveChangesPost.xml if the referencing component is being updated in the same deployment. Use destructiveChangesPre.xml or destructiveChanges.xml for standalone deletions. Never attempt to delete Record Types, Picklist values, or active Flow versions via manifest.
  4. Build the manifest — Author the XML with correct <types> and <members> entries. Do not include a <version> element. Pair with a valid companion package.xml (even if empty of components, it must specify the API version).
  5. Run the checker script — Execute python3 scripts/check_destructive.py --manifest-dir <path> to surface common authoring mistakes before deploying.
  6. Deploy and validate — Run sf project deploy start with the appropriate flag. Check the deployment status output. If errors occur, map them to the known failure modes in Pattern 3.
  7. Verify deletion in target org — Confirm the component no longer appears in Setup or metadata retrieval. Check that no unexpected cascading deletions occurred.

Review Checklist

Run through these before marking work in this area complete:


Salesforce-Specific Gotchas

Non-obvious platform behaviors that cause real production problems:

  1. Wildcards silently fail — Unlike package.xml, wildcards (*) in a destructive manifest do not delete all components of a type. The wildcard is treated as a literal member name and the deployment will either fail or silently skip the deletion. List every member explicitly.
  2. Post manifest required even when both changes are in the same deploy — Practitioners often assume that listing both the deletion and the updated referencing component in the same deployment is sufficient regardless of manifest type. The pre/plain variant fires the deletion first, before the updated component lands, causing the "still referenced" error even though the fix is in the same package.
  3. Deleted components may be re-created by a subsequent retrieve — If a team member runs sf project retrieve start after the deletion without updating their local source, the deleted component re-appears in the local project and may be re-deployed in the next push, undoing the deletion in the org.

Output Artifacts

| Artifact | Description |

|---|---|

| destructiveChangesPre.xml | Manifest of components to delete before additions; paired with package.xml |

| destructiveChangesPost.xml | Manifest of components to delete after additions; use when deleted component is still referenced by a component being updated in the same deploy |

| destructiveChanges.xml | Plain (simultaneous) variant; legacy format, still valid |

| package.xml (companion) | Required companion manifest; may contain no members but must specify the API version |


Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.