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
  • 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
4

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.

4

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

-1

public class CreateMetadata implements Metadata.DeployCallback {

public static void deleteMetadataRecord(String deleteRecord) {
    MetadataService.MetadataPort service = createService();
    List<String> recordsToDelete = new List<String>();
    recordsToDelete.add(deleteRecord);
    service.deleteMetadata('CustomMetadata', recordsToDelete);
}

public static MetadataService.MetadataPort createService() {
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();
    return service;
}

}

//Here you will fill Metadata object with record developer name To test // CreateMetadata.deleteMetadataRecord('Metadata_Creditional__mdt.MyTest');

1
  • 1
    Welcome to SFSE! Please take the tour and read How to Answer. This is very similar to the accepted answer, and contains no explanation. Providing code without an explanation of how it works or why decisions in code were made makes it a code dump. Code dumps are of limited value to the community because they don't help people understand why something is broken, and why the fix works. They also tend to generate a lot of back-and-forth in the comments like "that didn't work" and "try this instead" which makes it hard to follow.
    – Derek F
    Jun 10 at 16:20

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.