+ 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) 
Vinita_SFDCVinita_SFDC

Hello,

 

This code seems correct. You need to check the code from where are you passing the name of the custom fields to be deleted because it is directly taking variable name "Custom_fields_name" instead of the name of the custom field to be deleted.

 

Workaround is you can make use of destructivechanges.xml. For detail please refer:

 

http://wiki.developerforce.com/page/Propagating_Destructive_Changes

 

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

All Answers

amilawamilaw
Vinita_SFDCVinita_SFDC

Hello,

 

This code seems correct. You need to check the code from where are you passing the name of the custom fields to be deleted because it is directly taking variable name "Custom_fields_name" instead of the name of the custom field to be deleted.

 

Workaround is you can make use of destructivechanges.xml. For detail please refer:

 

http://wiki.developerforce.com/page/Propagating_Destructive_Changes

 

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

This was selected as the best answer