Your application can delete a collection in Microsoft System Center Configuration Manager 2007 by using the SMS_Collection Server WMI Class and class properties.

Caution
Care should be exercised when deleting any Configuration Manager 2007 object.

Collections are closely tied to packages, programs, and advertisements. For more information, see Software Distribution Overview.

These examples require the following values:

The following code is an example of the subroutine call in Visual Basic:

  Copy Code
Call DeleteCollection(swbemServices,"ABC00010")

The following code is an example of the method call in C#:

  Copy Code
DeleteCollection(WMIConnection,"ABC00010")

To delete a collection

  1. Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.

  2. Get the specific collection instance by using the collection ID provided.

  3. Delete the collection by using the delete method.

Example

The following example method deletes a collection.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

Visual Basic Script  Copy Code
' Setup a connection to the local provider.
Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
Set swbemServices= swbemLocator.ConnectServer(".", "root\sms")
Set providerLoc = swbemServices.InstancesOf("SMS_ProviderLocation")

For Each Location In providerLoc
	If location.ProviderForLocalSite = True Then
		Set swbemServices = swbemLocator.ConnectServer(Location.Machine, "root\sms\site_" + Location.SiteCode)
		Exit For
	End If
Next

Call DeleteCollection(swbemServices,"ABC00010")


Sub DeleteCollection(connection, collectionIDToDelete)

	' Get the specific collection instance to delete.
	Set collectionToDelete = connection.Get("SMS_Collection.CollectionID='" & collectionIDToDelete & "'")

	' Delete the collection.
	collectionToDelete.Delete_

	' Display change information.
	Wscript.Echo "Deleted collection: " & collectionIDToDelete

End Sub
C#  Copy Code
public void DeleteCollection(WqlConnectionManager connection, string collectionIDToDelete)
{
	//  Note:  On delete, the provider cleans up the SMS_CollectionSettings and SMS_CollectToSubCollect objects.
   
	try
	{
		// Get the specific collection instance to delete.
		IResultObject collectionToDelete = connection.GetInstance(@"SMS_Collection.CollectionID='" + collectionIDToDelete + "'");

		// Delete the collection.
		collectionToDelete.Delete();

		// Output the ID of the deleted collection.
		Console.WriteLine("Deleted collection: " + collectionIDToDelete);
}

	catch (SmsException ex)
	{
		Console.WriteLine("Failed to delete collection. Error: " + ex.Message);
		throw;
}
}

The example method has the following parameters:

Parameter Type Description

connection

swbemServices

  • Managed: WqlConnectionManager

  • VBScript: SWbemServices

A valid connection to the SMS Provider.

collectionIDToDelete

  • Managed: String

  • VBScript: String

Unique auto-generated ID containing eight characters. For more information, see the CollectionID property of SMS_Collection Server WMI Class.

Compiling the Code

The C# example requires:

Namespaces

System

System.Collections.Generic

System.ComponentModel

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

Robust Programming

For more information about error handling, see About Configuration Manager Errors.

Security

For more information about securing Configuration Manager applications, see Securing Configuration Manager Applications.

See Also


Send comments about this topic to Microsoft.