How to Delete a Collection
Applies To: System Center 2012 Configuration Manager, System Center 2012 Configuration Manager SP1, System Center 2012 R2 Configuration Manager
Your application can delete a collection in System Center 2012 R2 Configuration Manager by using the SMS_Collection Server WMI Class and class properties.
Warning
Care should be exercised when deleting any System Center 2012 R2 Configuration Manager 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
Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.
Get the specific collection instance by using the collection ID provided.
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 |
|
A valid connection to the SMS Provider. |
collectionIDToDelete |
|
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 Securing Configuration Manager Applications.
See Also
SMS_Collection Server WMI Class
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