The following example shows how to delete a package in System Center 2012 Configuration Manager by using the SMS_Package class.

Note
Any reference to this package, such as an advertisement or task sequence, should be cleaned up before deleting the package

To delete a package

  1. Set up a connection to the SMS Provider.

  2. Load the existing package object by using the SMS_Package class.

  3. Delete the package by using the delete method.

Example

The following example method deletes an existing package.

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

Visual Basic Script  Copy Code
Sub DeleteAPackage(connection, existingPackageID)

	' Get the specified package instance (passed in as existingPackageID).	Dim packageToDelete
	Set packageToDelete = connection.Get("SMS_Package.PackageID='" & existingPackageID & "'")

	' Delete the package.
	PackageToDelete.Delete_

	' Output package ID of deleted package.
	wscript.echo "Deleted Package ID: " & existingPackageID

End Sub
C#  Copy Code
public void DeleteAPackage(WqlConnectionManager connection, string existingPackageID)
{
	try
	{
		// Get the specified package instance (passed in as existingPackageID).
		IResultObject packageToDelete = connection.GetInstance(@"SMS_Package.PackageID='" + existingPackageID + "'");

		// Delete the package instance.
		packageToDelete.Delete();

		// Output package ID of deleted package.
		Console.WriteLine("Deleted Package ID: " + existingPackageID);
}

	catch (SmsException ex)
	{
		Console.WriteLine("Failed to create package. 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.

existingPackageID

  • Managed: String

  • VBScript: String

The ID of the existing package.

Compiling the Code

The C# example requires:

Namespaces

System

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

mscorlib

Robust Programming

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

Security

See Also