How to Delete a Package
Applies To: System Center 2012 Configuration Manager, System Center 2012 Configuration Manager SP1, System Center 2012 R2 Configuration Manager
The following example shows how to delete a package in System Center 2012 R2 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
Set up a connection to the SMS Provider.
Load the existing package object by using the SMS_Package class.
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.
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
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 |
|
A valid connection to the SMS Provider. |
existingPackageID |
|
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.
.NET Framework Security
See Also
Configuration Manager Software Distribution
Software Distribution Packages
SMS_SCI_Component Server WMI Class