Many Awesome Admins know that you can use Eclipse and Force.com IDE to create and edit Salesforce metadata. What many of us don’t realize is that you can also use these tools to mass delete fields, objects, list views, etc. If you ever tried to get rid of a high number of old fields, for example, you will understand how hard it can be, especially if they are referenced somewhere else. Now imagine that you need to delete 30 or 40 fields and half of them are referenced in workflows, validation rules or even Apex classes. You will see an error message when you try to delete each of these fields. And you will have to spend a lot of time completing this task manually.
Today using old fields as an example, I am going to show you how to mass delete metadata in Eclipse.
I have identified two fields: ‘Education’ and ‘Email’ that need to be deleted and I am going to use Eclipse and Force.com IDE to complete this operation.
To add or edit fields in Eclipse, you open a metadata file for a specified object, add/edit .xml code and then save changes into your sandbox or production org. But what will happen if you delete .xml code for the above two fields from a metadata file? Will it automatically delete the fields from your Salesforce org once you click Save to Server?
The answer is No. If you delete .xml code from a metadata file, Salesforce will simply ignore these fields when you click Save to Server (or Deploy to Server). How can you delete fields, list views, validation rules, workflows, etc. in this case? You can do it by running Destructive Changes.
Destructive Changes is an .xml file that contains information about metadata you want to delete. List elements you are trying to get rid of, save the file and run a standard script (you don’t need to do any coding) that will do the job. DestructiveChanges.xml looks like this:
<types></types> tags separate metadata types, such as custom fields, custom objects, list views, etc. On the above image you can see separate sets of <types></types> for custom fields, custom objects and list views.
<name></name> tags contain the name of the component type.
<members></members> tags list individual components you want to delete.
Let’s go through the process step by step.
- Install Eclipse and Force.com IDE, if you haven’t done it yet. You can find a video with instructions on this web-site.
- Create a Force.com project. You don’t have to include any metadata; however, it is advised to include metadata that can be affected by deletion (including elements that you are going to delete), in case something goes wrong.
- Download ‘build.zip’ archive from here and extract files into the same folder where ‘src’ folder of your project is located.
- Go to Eclipse, click on your project and click F5 to refresh. You should see ‘build’ folder there once you complete this.
- Double-click ‘build.properties’ file to open it, type your username and password for a production org or a sandbox. If you are going to delete metadata from a sandbox, replace login.salesforce.com in the first line with test.salesforce.com. Save the changes.
- Edit destructiveChanges.xml file and save it.
I have added ‘Education’ and ‘Email’ fields to my destructiveChanges.xml file. As both of these components are fields, I added ‘CustomField’ between <name></name> tags.
If you don’t know what to add between <name></name>, include a component you want to delete into package.xml by clicking Project Name -> Force.com -> Add/Remove Metadata Components… and copy the name from there. From an example below you can see what to add between <name></name> tags for Approval Processes, Assignment Rules, Auto-Response Rules, Custom Objects and Custom Tabs.
- Run the script to delete metadata from your org.
- Right-click Build.xml
- Click Run As
- Select 2 Ant Build
- Tick destructiveChanges check-box and click Run
- Once operation is completed, you should see Build Successful message in the Console.
If you open Salesforce, you will notice the following:
- Fields have been permanently deleted and you cannot undelete them from Setup
- There is a record about a successful deployment
- A record about deleted fields added to Setup Audit Trail
- What if a field is referenced in other metadata? Will it still be deleted? The answer is No.
If at least one element from destructive changes cannot be deleted, the whole operation will fail. No metadata will be deleted. You will see descriptive error messages and once you resolve them, you will need to run destructive changes again to complete the operation.
For example, I want to delete a custom Contact field called ‘Budget’. ‘Budget’ is referenced in another field. I have added ‘Budget’ to destructiveChanges.xml and have run the script.
I can now see an error message in Console and on Deployment Status page within Salesforce Setup.
Once I remove a reference to ‘Budget’ field, I’ll be able to run destructiveChanges.xml again and complete deletion.
Things to remember:
A successful deletion is permanent. You won’t be able to undelete metadata.
The best practice is to try this in a sandbox before you do it in production, at least until you become comfortable with this sort of tasks.