Wednesday, June 15, 2022

How to Delete (Bulk) Custom Metadata Records in Salesforce.

 Scenario:

You might have used the "Custom Metadata Loader" to perform the bulk insert and bulk update. Sometimes it's might be required to perform the bulk delete also ,but unfortunately this tool doesn't support as of today.

Solution:

So as a workaround we can have apex code snippet which can be used to perform the bulk deletion(in a single run max 200 records) of metadata .Please use the below code snippet for the same.

Source Code:

MetadataService.MetadataPort service = new MetadataService.MetadataPort();

// Set the session id
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();

//Add all your metadata records developer name to list
List<String> recordsToDelete = new List<String>();
For(YourMedataDataName__mdt m :[SELECT Developername from YourMedataDataName__mdt Limit 200])
{
	String s = 'YourMedataDataName__mdt.'+m.Developername;
	recordsToDelete.add(s);
}

//Perform the bulk deletion at a time max of 200 records
MetadataService.DeleteResult [] results = new MetadataService.DeleteResult []{};
results = service.deleteMetadata('CustomMetadata', recordsToDelete);

Please comment or write us if you have any queries/requirements.

Please like,follow,bookmark,subscribe this site to receive daily updates.


FaceBook Page - I Love Coding. You?


Hope this helps you..Enjoy..!

3 comments:

  1. i get the error : Invalid type: MetadataService.MetadataPort

    ReplyDelete
    Replies
    1. Please deploy this to salesforce instance.
      https://github.com/financialforcedev/apex-mdapi/blob/master/README.markdown

      Delete