Sunday, August 12, 2012

Salesforce Deployment - Delete a file through ANT

I usually use the ant for deployment this is easy to use & also take less time then Eclipse deployment. Recently  I required to delete a resource from some salesforec org, & I found that there is no detail mentioned about this topic on internet. I keep digging this & come with the detail how you can delete a resource by ANT.
When we use ant deployment here is the folder hierarchy which we use-
ANT Sample Folder
                >>mypkg Folder
                >>retrieveUnpackaged Folder
                >> removecodepkg Folder [Create this new folder to delete the files]
                >>build.properties files
                >>build.xml

To delete a resource you need to add a destructiveChanges.xml  file in removecodepkg  folder. The destructiveChanges.xml file is same as package.xml. format of this file is as below-

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>LoginController</members>
        <members>LoginControllerTest</members>
        <name>ApexClass</name>
    </types>      
   <version>19.0</version>
</Package>

You also need to have a blank package.xml file in the same removecodepkg  folder. Format of the file will be-

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
  <version>19.0</version>
</Package>

Now you have two xml files in removecodepkg  folder –
1-destructiveChanges.xml  
 2-package.xml
  
ant is having build.xml file which have all commands can be executed. There will be a command to delete the code-

<target name="undeployCode">

      <sf:deploy username="${sf.username}" password="${sf.password}" serverurl="${sf.serverurl}" deployRoot="removecodepkg"/>
    </target>

  
Now mention the name of the resource in destructiveChanges.xml  file & delete the file from destination org. in our example I am deleting a class LoginController & it’s test class.
You just need to execute below command on ant
>>ant undeployCode
       


1 comment: