[SalesForce] How to delete Custom MetaData Records Via Apex

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?

Best Answer

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.

Related Topic