Close Sun Nov 18 13:54:19 GMT 2018

Destroy all Salesforce Metadata with Ant

Update: this is less applicable if using SFDX scratch orgs.

This script searches and destroys nearly all metadata in an organization. Use cases include ISV package development and testing within Developer Edition and Sandbox environments.

Usage:

First paste this macrodef into your build.xml

Then the macro may be called by any build target:

<target name="destroy">
    <destroy
        username="${sf.username}"
        password="${sf.password}"
        serverurl="${sf.serverurl}"
    />
</target>

It forces an input prompt for the URL and username:

>
> THIS TASK IRREVERSIBLY DESTROYS ALL METADATA. ARE YOU SURE?
> (https://test.salesforce.com/?un=username@organization...)
>

How does it work?

First sf:describeMetadata determines the "shape" of the organization. Then, all components are listed in turn from each metadata type. The results are appended in a destructiveChangesPost.xml manifest which is then deployed back over the org.

Does it unlink dependencies?

Yes. Dependencies are cleared in one metadata deployment. It takes advantage of destructive changes POST-deploy, which clears everything in a single transaction. Specifically:

  • Page Layouts are cleared of custom buttons and custom links
  • Sites are cleared of static resources and visualforce pages
  • Profiles are cleared of default application visibilities
  • Roles are cleared of their parent hierarchy association

What cannot be destroyed?

Some metadata types such as Assignment Rules act as permanent containers for the individual rules. Other metadata types such as Record Types do not support the delete operation. These problematic types fall into three categories.

Containers that cannot be destroyed:

  • Assignment Rules
  • AutoResponse Rules
  • Escalation Rules
  • Matching Rules
  • Sharing Rules
  • Workflow Rules

Standard components that cannot be destroyed:

  • Field
  • Object
  • Layout
  • Profile
  • App Menu
  • Matching Rule
  • TopicsForObjects
  • Clean Data Service
  • Business Process as in Case record type
  • Community as in Idea Zone, not to be confused with "Network"

Custom components that shouldn't be destroyed:

  • Force.com Sites (permanent and cannot be destroyed)
  • Flow (destroy the independent FlowDefinitions instead)
  • Certificate (these cannot be regenerated, leave them intact)
  • ConnectedApp (to avoid interfering with consumer secrets etc)
  • CustomObjectTranslation (these are deleted anyway when object is deleted)
  • InstalledPackage (best uninstalled via UI which nags about extensions etc)
  • RecordType (must be destroyed in UI; it starts an async background process)

Other unsupported metadata types are documented in the Metadata API guide

What about Custom Metadata Types?

This script destroys both the named metadata records as well as the custom definition.



Related