How to Delete a Group

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

You must know the name of the group you want to delete, and then you can delete the group from the management pack that contains the group. The following code example deletes a group named SampleGroup1. The DeleteMonitoringObjectGroup method is used to delete the group.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Monitoring;

namespace SDKSamples
{
	class Program
	{
		static void Main(string[] args)
		{
			ManagementGroup mg = new ManagementGroup("localhost");

			try
			{
				DeleteGroup(mg, "SampleGroup1");
		}
			catch (EnterpriseManagementException error)
			{
				Console.WriteLine(error.Message);
		}
	}

		// ---------------------------------------------------------------------
		private static void DeleteGroup(
			ManagementGroup mg,
			string groupDisplayName
			)
		{
			IList<MonitoringObjectGroup> groups;
			MonitoringObjectGroup groupToDelete = null;

			groups = mg.EntityObjects.GetRootObjectGroups<MonitoringObjectGroup>(ObjectQueryOptions.Default);

			foreach (MonitoringObjectGroup group in groups)
			{
				if (group.DisplayName == groupDisplayName)
				{
					groupToDelete = group;
					break;
			}
		}

			if (groupToDelete == null)
			{
				throw new ApplicationException("Failed to retrieve the specified group");
		}
			else
			{
				IList<ManagementPackClass> monitoringClasses = groupToDelete.GetClasses();
				monitoringClasses[0].GetManagementPack().DeleteEnterpriseManagementObjectGroup(groupToDelete);
		}
	}
}
}

See Also