Function: vCenter VAPI Plugin Detach All Tags By Category From vCenter Virtual Machine

by Simon Sparks · 13 January 2026

To use this function add it to the class library file named vCenterVAPIService.ts

GitHub Repository: https://github.com/SimonSparksUK/Orchestrator

Filename: vCenterVAPIService.ts

Function detachMultipleTagsFromVirtualMachine

private detachMultipleTagsFromVirtualMachine(objVAPIClient: VAPIClient, arrTagID: string[], objVcVirtualMachine: VcVirtualMachine): com_vmware_cis_tagging_tag__association_batch__result {
    let objDynamicID: com_vmware_vapi_std_dynamic__ID = this.getDynamicIDFromVirtualMachine(objVcVirtualMachine);

    let objVAPITaggingTagAssociation: com_vmware_cis_tagging_tag__association = new com_vmware_cis_tagging_tag__association(objVAPIClient);

    let objVMwareCISTaggingTagAssociationBatchResult: com_vmware_cis_tagging_tag__association_batch__result = objVAPITaggingTagAssociation.detach_multiple_tags_from_object(objDynamicID, arrTagID);

    return objVMwareCISTaggingTagAssociationBatchResult;
}

Function detachAllTagFromGivenTagCategory

public detachAllTagFromGivenTagCategory(objVcVirtualMachine: VcVirtualMachine, strTagCategoryName: string): void {
    this.objLogger.info(`Start - Detaching all Tags from Tag Category '${strTagCategoryName}' from Virtual Machine '${objVcVirtualMachine.name}'`);

    let objVcSdkConnection: VcSdkConnection = objVcVirtualMachine.sdkConnection;

    let strVCenterServerName: string = objVcSdkConnection.id;

    let objVAPIClient: VAPIClient;

    try {
        let objVAPIEndpoint: VAPIEndpoint = VAPIManager.findEndpoint(`https://${strVCenterServerName}/api`);

        objVAPIClient = objVAPIEndpoint.client(null, null);

        let strTagCategoryID: string = this.findTagCategoryIDByName(objVAPIClient, strTagCategoryName);

        let arrTagID: string[] = this.listAttachedTagsFromSpecificCategoryID(objVAPIClient, objVcVirtualMachine, strTagCategoryID);

        this.objLogger.info(`===== Project '${this.strProjectID}' - vCenter Inventory on '${strVCenterServerName}' has a Virtual Machine named '${objVcVirtualMachine.name}' - Attempting to detach vCenter Tags from Tag Category '${strTagCategoryName}'...`);

        this.detachMultipleTagsFromVirtualMachine(objVAPIClient, arrTagID, objVcVirtualMachine);

        this.objLogger.info(`===== Project '${this.strProjectID}' - vCenter Inventory on '${strVCenterServerName}' has a Virtual Machine named '${objVcVirtualMachine.name}' - Successfully detached vCenter Tag from Tag Category '${strTagCategoryName}'.`);

    } catch (objException) {
        this.objLogger.error(`===== Project '${this.strProjectID}' - vCenter Inventory on '${strVCenterServerName}' has a Virtual Machine named '${objVcVirtualMachine.name}' - Failed to detached vCenter Tag from Tag Category '${strTagCategoryName}'.`);
    } finally {
        objVAPIClient.close();
    }

    this.objLogger.info(`End - Detaching all Tags from Tag Category '${strTagCategoryName}' from Virtual Machine '${objVcVirtualMachine.name}'`);
}

Discover more from Cloud Build Tools

Subscribe to get the latest posts sent to your email.

You may also like