Edit

Share via


How to Delete a Collection

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

Important

  • Care should be exercised when deleting any Configuration Manager object.

  • We recommend that if you are deleting several collections, you do so one at a time, to allow database operations time to manage changes associated with the deletions.

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 SMS Provider fundamentals.

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

.NET Framework Security

For more information about securing Configuration Manager applications, see Configuration Manager role-based administration.

See Also

SMS_Collection Server WMI Class
Delete a collection Software distribution overview About deployments Objects overview How to Connect to a Configuration Manager Provider using Managed Code
How to Connect to a Configuration Manager Provider Using WMI