function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dhaval PanchalDhaval Panchal 

How to remove custom fields using Metadata API (Bulk delete custom fields)

Hello,

 

I want to delete 500+ custom fields. Is there any solution to mass remove custom fields? I have tried with metadata Api using below code. But it gives me below error.

 

Object with id:04s900000037qo4AAA is InProgress
Error status code: INVALID_CROSS_REFERENCE_KEY
Error message: In field: members - no CustomField named Custom_Field__c found
Object with id:04s900000037qo4AAA is Error

 

Below is the code:

 


    public void deleteCustomField(String fullname) throws Exception
    {
        CustomField customField = new CustomField();
        customField.setFullName(fullname);
        
        UpdateMetadata updateMetadata = new UpdateMetadata();
        updateMetadata.setMetadata(customField);
        updateMetadata.setCurrentName(fullname);
        
        AsyncResult[] asyncResults  = metadataConnection.delete(new Metadata[] {customField});
 
        long waitTimeMilliSecs = ONE_SECOND;
 
        do
        {
            printAsyncResultStatus(asyncResults);
            waitTimeMilliSecs *= 2;
            Thread.sleep(waitTimeMilliSecs);
            asyncResults = metadataConnection.checkStatus(new String[]{asyncResults[0].getId()});
        } while (!asyncResults[0].isDone());
 
        printAsyncResultStatus(asyncResults);
    }

 


    private void printAsyncResultStatus(AsyncResult[] asyncResults) throws Exception {
        if (asyncResults == null || asyncResults.length == 0 || asyncResults[0] == null) {
            throw new Exception("The object status cannot be retrieved");
        }

        AsyncResult asyncResult = asyncResults[0]; //we are creating only 1 metadata object

        if (asyncResult.getStatusCode() != null) {
            System.out.println("Error status code: " +
                    asyncResult.getStatusCode());
            System.out.println("Error message: " + asyncResult.getMessage());
        }

        System.out.println("Object with id:" + asyncResult.getId() + " is " +
            asyncResult.getState());
    }

 

Is there any other solution (any app) to removing custom fields?

 

Thanks in advance,

 

Dhaval Panchal

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

Using ANT and SFDC Migration tool I am now able to delete bulk custom fields.

Download Apache Ant using below link:

Extract and copy this folder where you want to keetp (i have copied this folder to "C" drive : C:\apache-ant-1.9.2)
Set path in environment variables for ant.
To set environment variables
-> Right click on My Computer -> Property -> Advance System Setting -> Click on Environment variables button.
-> click on System variables -> Click on New -> Give variable name = ANT_HOME and variable value = [your and path]
see below images.


Now go to your salesforce account
click on
Setup -> Develop -> Tools -> Force.com Migration Tool.
Download "salesforce_ant_28.0" and extract this zip file.
You will file "ant-salesforce.jar" file in extracted folder, copy this
jar file to "C:\apache-ant-1.9.2\lib"

Now go to folder salesforce_ant_28.0 -> sample
open file "build.properties" and provide your username, password and token there.
In sample folder you will find xml file formates using that you can deploy components.

All Answers

amarcuteamarcute

Hi,

 

There is no solution I am aware of to delete custom fields programmatically. You cannot delete/remove fields using metedata API directly.

But check out this Thread, for a work around:

 

http://boards.developerforce.com/t5/General-Development/Delete-custom-fields-with-metadata-api-eclipse/td-p/165945

 

 

Dhaval PanchalDhaval Panchal

Using ANT and SFDC Migration tool I am now able to delete bulk custom fields.

Download Apache Ant using below link:

Extract and copy this folder where you want to keetp (i have copied this folder to "C" drive : C:\apache-ant-1.9.2)
Set path in environment variables for ant.
To set environment variables
-> Right click on My Computer -> Property -> Advance System Setting -> Click on Environment variables button.
-> click on System variables -> Click on New -> Give variable name = ANT_HOME and variable value = [your and path]
see below images.


Now go to your salesforce account
click on
Setup -> Develop -> Tools -> Force.com Migration Tool.
Download "salesforce_ant_28.0" and extract this zip file.
You will file "ant-salesforce.jar" file in extracted folder, copy this
jar file to "C:\apache-ant-1.9.2\lib"

Now go to folder salesforce_ant_28.0 -> sample
open file "build.properties" and provide your username, password and token there.
In sample folder you will find xml file formates using that you can deploy components.

This was selected as the best answer
aditya3aditya3
Then what is the solution  for this problem ?please let me know
Daniel Lane 3Daniel Lane 3
Actually this is a mistake on the original posters part. He needs to fully qualify the field. e.g. vendorprefix__customobject__c.vendorprefix__customField__c. The metadata API works perfectly fine for this.
Mohit Bansal 13Mohit Bansal 13
Here is the Salesforce AppExchange app "BULK OBJECT FIELD CREATOR" for this requirement:

About BOFC:
Helps Admins & Developers to CREATE multiple, DELETE multiple fields, EXPORT Object - Fields Detail & Assign FLS for multiple profiles for multiple fields in single click 

User-added image

Features:
  • Create Bulk(multiple) Fields in Single Click
  • Update Bulk(multiple) Fields in Single Click
  • Delete Bulk(multiple) Fields in Single Click
  • Export Object & its fields details in single click in form of excel
  • Assign Field Level Security for multiple profiles for multiple fields in Single Click

For AppExchange: 
https://appexchange.salesforce.com/listingDetail?listingId=a0N30000000qDqqEAE 

Kindly share your valuable inputs/feedback or let me know, if you need any assistance. 

You can contact me:
Email: mishu67777@gmail.com
Mobile: +91-9953170767