5

Long story, but I want to clear all the code from an org. I don't need to clear Visualforce pages (there is only one, and I'm deleting that manually) - just all triggers and Apex classes. Does anyone have a quick way to do it?

I have a feeling that it will involve using Workbench, but I've never tried it so I'm coming here before doing anything with that.

Using the UI and deleting triggers and classes one by one is not an option.

  • 1
    If you want to keep VF pages you may want to specify how you want to deal with classes that are VF controllers, or classes that are used from VF controllers. – Samuel De Rycke Apr 14 '14 at 18:43
  • Done. Good idea. I have only one page, which I'm deleting manually. No need to overcomplicate this ugly process! – DavidSchach Apr 14 '14 at 18:44
  • btw: my guess is on the tooling api, but I don't know it well enough to shape that into an answer. – Samuel De Rycke Apr 14 '14 at 18:46
  • Do you have any Force.com Sites set up in the org @DavidSchach? – bigassforce Apr 14 '14 at 21:50
  • No sites. It is a pretty sparse org. – DavidSchach Apr 14 '14 at 22:08
3

Here's a way using just the Salesforce-supported tool and no dependencies. Proceed with caution ;-)

  1. create a temporary directory to work in,

  2. download salesforce_ant_29.0.zip and extract into a subdirectory lib/salesforce_ant_29.0

  3. create a file in your temp directory called build.properties containing the following:

    sf.username=username@example.com
    sf.password=passwordandsecuritytoken
    
  4. create another file in the temp directory called build.xml, then cd in there and run ant purge:

    <taskdef uri="antlib:com.salesforce" classpath="lib/salesforce_ant_29.0/ant-salesforce.jar" />
    <property file="build.properties"/>
    <property environment="env"/>
    
    <target name="purge" description="PURGE TRIGGERS AND CLASSES">
        <!-- Clean up any previous purge -->
        <delete dir="purge" />
        <mkdir dir="purge" />
    
        <!-- Prepare a wildcard package definition with as many metadata types as possible -->
        <echo file="purge/package.xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
            <Package xmlns="http://soap.sforce.com/2006/04/metadata">
                <types><members>*</members><name>ApexClass</name></types>
                <types><members>*</members><name>ApexTrigger</name></types>
                <version>29.0</version>
            </Package>
        ]]></echo>
    
        <!-- Retrieve the wildcard package definition -->
        <sf:retrieve
        username="${sf.username}"
        password="${sf.password}"
        serverurl="https://login.salesforce.com"
        retrieveTarget="purge"
        unpackaged="purge/package.xml"
        pollWaitMillis="1000" />
    
        <!-- Name the package definition -->
        <replace file="purge/package.xml">
            <replacetoken><![CDATA[</version>]]></replacetoken>
            <replacevalue><![CDATA[</version><fullName>Purge</fullName>]]></replacevalue>
        </replace>
    
        <!-- Re-deploy the now-named wildcard package definition -->
        <sf:deploy
        username="${sf.username}"
        password="${sf.password}"
        serverurl="https://login.salesforce.com"
        deployRoot="purge"
        purgeOnDelete="true"
        pollWaitMillis="1000" />
    
        <!-- Retrieve by name, giving us an explicit package definition -->
        <sf:retrieve
        username="${sf.username}"
        password="${sf.password}"
        serverurl="https://login.salesforce.com"
        retrieveTarget="purge"
        packageNames="Purge"
        pollWaitMillis="1000" />
    
        <!--
         ! Trash local components: we only needed them to get an explicit package definition.
         ! Now we turn the explicit package definition into explicit destructive changes, and
         ! blank out the package definition.
         !-->
        <delete includeEmptyDirs="true" dir="purge" includes="**/*" excludes="*.xml" />
        <copy file="purge/package.xml" tofile="purge/destructiveChanges.xml" />
        <echo file="purge/package.xml"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
            <Package xmlns="http://soap.sforce.com/2006/04/metadata">
                <version>29.0</version>
            </Package>
        ]]></echo>
    
        <!-- Perform explicit deploy over the top of the org which should delete all components -->
        <sf:deploy
        username="${sf.username}"
        password="${sf.password}"
        serverurl="https://login.salesforce.com"
        deployRoot="purge"
        purgeOnDelete="true"
        pollWaitMillis="1000" />
    
        <!-- Tidy up after ourselves -->
        <delete dir="purge" />
    </target>
    

3

FinancialForce open sourced a tool we use internally to undeploy everything from an org. You need to invoke it via ant, and it goes above and beyond your needs (also removing objects, static resources, etc). I suspect it's your best bet here.

https://github.com/financialforcedev/df12-deployment-tools

1

Using Eclipse to delete classes by one time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.