Search code examples
c#salesforce

Hard delete custom field in Salesforce


I am having problem in deleting custom fields permanently.

Like for e.g. I have created a custom field in Contact entity with name "Newsletter" which Salesforce internaly stores as "Newsletter__c" as custom field.

Then I use the below code to delete custom field of contact.

var cstField = new CustomField
{
  type = FieldType.Checkbox,
  fullName = "Contact.Newsletter__c"
};

// Delete the object
var r = metaService.delete(new Metadata[] { cstField })[0];

The above code deletes the custom field, but keeps it under "DeletedFields" category where you can again "Erase" or "Undelete" the custom field. These custom fields are deleted automatically after 15 days.

I want to delete the custom fields from these category also as if I again create cf with same name SF gives error like "Already exists".

I tried purgeOnDelete option too while deploying but no luck so far.


Solution

  • purgeOnDelete. If true, the deleted components in the destructiveChanges.xml manifest file aren't stored in the Recycle Bin. Instead, they become immediately eligible for deletion. This field is available in API version 22.0 and later. This option only works in Developer Edition or sandbox organizations; it doesn't work in production organizations. Extending Salesforce Migration Tool to Support PurgeOnDelete.. For some reason this excellent feature has still not been exposed by the Salesforce Migration Tools via the sf:deploy Ant Task. However with a bit of Java skills you can create a new deploy Ant Task by extending the current one, to expose the attribute to your Ant build scripts, for example...

    Prebuilt ant-salesforce.jar with deploypurge in it. You can download a modified ant-salesforce.jar here from the FinancialForce.com Developers Github repo (be warned this is API v22.0, though you can use it to deploy code at any version).

    Building your own extended ant-salesforce.jar. If you want rebuild a new version, then you need to download this Java class, compile it, unzip the ant-salesforce.jar (rename .jar to .zip), put it in and zip it back up again and your all set!

    Refer : https://salesforce.stackexchange.com/questions/12709/hard-delete-objects-using-the-force-com-migration-tool