To delete a System Center 2012 R2 Configuration Manager object by using the managed SMS Provider, use the IResultObject.Delete method. You can get a IResultObject object for a System Center 2012 R2 Configuration Manager object in numerous ways. For more information, see How to Read a Configuration Manager Object by Using Managed Code

To delete a Configuration Manager object

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

  2. Using the WqlConnectionManager object you obtain in step one, call the GetInstance method to get the IResultObject object for the Configuration Manager object.

  3. Call the IResultObject object Delete method to delete the Configuration Manager object.

Example

The following example deletes a package by using the supplied package identifier. This example uses the WqlConnectionManager class GetInstance method to get an IResultObject object for the Configuration Manager package and then deletes the package.

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

  Copy Code
public void DeletePackage(WqlConnectionManager connection, string packageID)
{
	try
	{
		IResultObject package = connection.GetInstance(@"SMS_Package.PackageID='" + packageID + "'");
		package.Delete();
}
	catch (SmsException ex)
	{
		Console.WriteLine("Failed to delete package: " + ex.Message);
		throw;
}
}

This example method has the following parameters:

Parameter Type Description

connection

  • WqlConnectionManager

A valid connection to the SMS Provider.

PackageID

  • String

The package identifier for an existing package. This can be obtained from the SMS_Package class PackageID property.

Compiling the Code

Namespaces

System

System.Collections.Generic

System.ComponentModel

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

Robust Programming

The Configuration Manager exceptions that can be raised are SmsConnectionException and SmsQueryException. These can be caught together with SmsException.

See Also