I am attempting to remove some custom metadata, I am able to deploy it, and view a successful result, but when I create a new Metadata.DeployContainer container, and use the container.removeMetadata or container.removeMetadataByFullname method the data remains (and is not marked as deleted in deployment result callback)?

List<String> dpmFullnames = new List<String>{'MyMetaDataName.lasZ', 'MyMetaDataName.zNkrh', 'MyMetaDataName.zNkA'}; 
List<String> dpmDontRemove = new List<String>{'lasZ'};

Metadata.DeployContainer container = new Metadata.DeployContainer();

List<Metadata.Metadata> savedCustomMetadata = 

 for(Metadata.Metadata dpm : savedCustomMetadata){
Metadata.CustomMetadata dpmRecord = (Metadata.CustomMetadata) dpm;
     String recordName = dpmRecord.fullName.substringAfter('.');
         System.debug('Remove '+recordName);
     } else {
         System.debug('Add '+recordName);

SonarCustomMetaDataDeployCallback callback = new SonarCustomMetaDataDeployCallback();
Id asyncResultId = Metadata.Operations.enqueueDeployment(container, callback);   

If I remove all the data, I get an error that I can not deploy an empty container. Assuming it is just removing them, not setting them as "deleted"?


My understanding is that the Metadata.DeployContainer is the collection of components you want to deploy via Apex.

When you call removeMetadata(md) or removeMetadataByFullName(fullName) you are saying "Don't include those components in the collection if they are there" rather than "These are the components I want to delete".

As it is currently (Spring '18 / v42.0) the Metadata via Apex doesn't support deleting Metadata. Instead, that needs to be handled via destructivechanges.xml against the actual Metadata API.

You can see discussion on the Apex Metadata support in the Trailblazer community group - Apex Metadata API.

From the current group description:

Note that we plan to add support for “Delete,” but it is not the top priority.


Are there any plans to support "delete" operation in Apex Metadata API?
- Eugene Rusalev

Yes, we do plan to support it, but it isn't imminent. Adding support for more metadata types is higher on the list.
- Aaron Slettehaugh (Salesforce)

  • Indeed, this is why my team's new managed package still uses Fawcett's Metadata API wrapper instead of the new Apex API. We need to be able to create and delete, and in particular to do it synchronously. – Charles T Apr 22 '18 at 23:49

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.