How to Delete a Collection

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

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

Warning

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:

  • A Windows Management Instrumentation (WMI) connection object.

  • An existing collection ID.

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

Call DeleteCollection(swbemServices,"ABC00010")

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

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.

' 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
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

Reference

SMS_Collection Server WMI Class

Concepts

Configuration Manager Collections
Configuration Manager Software Distribution
Software Distribution Packages
Software Distribution Programs
Software Distribution Advertisements
How to Use Configuration Manager Objects with WMI
How to Use Configuration Manager Objects with Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using WMI