2

I currently have 100+ records I need to delete under my custom metadata type, and I am not keen on having to do it one by one (I will need to reset the records every now and then as our requirements keep updating). I tried to do this via Apex but according to the documentation:

Apex code can create, read, and update (but not delete) custom metadata records...

2
  • have you tried data loader to delete?
    – sfdcFanBoy
    Nov 29, 2017 at 4:28
  • @sfdcFanBoy No, but my research tells me that it doesn't support custom metadata types. I've used the Custom Metadata Loader tool (github.com/forcedotcom/CustomMetadataLoader) but it doesn't have a delete function.
    – unhinged
    Nov 29, 2017 at 5:42

2 Answers 2

8

As Custom Metadata Records are metadata you can use Force.com Migration Tool and deploy the destructive changes

content of destructiveChanges.xml

<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>CustomMetadataType.CustomMetadataTypeRecord</members>
        <name>CustomMetadata</name>
    </types>
</Package>
1
  • I have tried to delete it with destructive XML but still it remains unchanged, what part I am missing? I have followed below points: 1. In Vs code retrieved the metadata record. 2. Put the record in destructive changes XML. 3. Remove the components from package.xml. 4. Deploy the destructive changes.xml Jul 25, 2023 at 8:01
2

If you can run Apex, here's how you assemble the list for the destructiveChanges.xml very quick. Note: replace { and } by corresponding angle brackets. Copy paste the debug statements into your destructiveChanges.xml file.

List{String} objectTypes = new List{String}{
        SomeCustomMetadataType__mdt.getSObjectType().getDescribe().getName(),
        AnotherCustomMetadataType__mdt.getSObjectType().getDescribe().getName()
};





System.debug('\t{types}');
for (String objectType : objectTypes) {
    List{SObject} customMetadataRecords = Database.query('SELECT DeveloperName FROM ' + objectType);
    
    for (SObject customMetadataRecord : customMetadataRecords) {
        String recordName = (String)customMetadataRecord.get('DeveloperName');
        System.debug('\t\t{members}' + objectType + '.' + recordName + '{/members}');
    }
}
System.debug('\t\t{name}CustomMetadata{/name}');
System.debug('\t{/types}');

2
  • Thanks, for your code, I'm able to generate descructiveChange.xml and execute undeployCode comment. Maybe you can just change your sample replace "{xx}" to "<xx>", since we are dealing with XML not JSON.
    – Estin Chin
    Apr 30, 2021 at 4:49
  • Angle brackets are interpreted as html by the editor, hence the curly brackets. Nov 18, 2022 at 10:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .