1

I am using MetaDataService to create and update Custom Metadata.

I want to also delete records created inside Custom Metadata Type.

I know there is method available to delete metadata like I am using following:

MetadataService.MetadataPort service = new  MetadataService.MetadataPort();
MetadataService.DeleteResult [] results = new MetadataService.DeleteResult []{};
results =service.deleteMetadata('CustomMetadata',new List<String>(API_NAME_OF_METADATA));

Here we need to provide two arguments to deleteMetadata method of MetadataService:

  1. Type
  2. List - List of API Names

I have Test_MetaData__mdt and inside that I have 5 records. Now, If I have to delete 5 records of Test_MetaData__mdt which type or what arguments should I provide to MetadataService so that I can delete those 5 records?

MetaDataService Delete method

public MetadataService.DeleteResult[] deleteMetadata(String type_x,String[] fullNames) {

So

results =service.deleteMetadata('Which Type?',new List<String>(What Values?));

What values do I need to use for the API Name? Or is it possible to delete custom metadata records via Apex?

  • 1
    Please do not post clarifications in the comments. Instead, you should edit your post to be more clear about what you are asking. I have done so for you here. – Adrian Larson Aug 20 '18 at 18:00
2

Yes, it is possible to delete metadata records using MetadataService.deleteMetadata method.

Here is a sample method call that should work:

List<String> recordsToDelete = new List<String>();
recordsToDelete.add('My_Custom_Type.record1');
service.deleteMetadata('CustomMetadata', recordsToDelete);

where My_Custom_Type is the custom metadata type whose record you are trying to delete.

1

Right now, I don't think it is possible to delete metadata records via Apex - only create/update

From the Trailhead Docs

Although there’s a lot you can do with Apex Metadata API, there are some > limitations to keep in mind.

In the current release, we support only two metadata types: page layouts and the records of custom metadata types. Reading, creating, and updating metadata are supported, but deleting metadata is not supported.

The Salesforce Foundation has done a lot of work with Apex and Custom Metadata recently, and their approach was to add a flag to every record called 'Is Deleted', update that to true when a user wanted to mark something as deleted, and then suppress those records when they were being accessed via a query - so basically a custom soft delete. A user can still delete via regular CMD Setup UI if they aren't protected, and /or I think the regular metadata API will still allow deletions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.