Ensure that all objects are initialized, the appropriate variables are used and open objects are closed at the end.
The sample code below illustrates how you would write code to remove a line item from a specific Purchase Order. 

Example of deleting a line item for a specific purchase order:
Public Sub DeletePOLineItem()
    Dim oPurchaseOrder As CAPPurchaseOrder
    Dim oLineItems As cAPLineItems
    Dim oLineItem As CAPLineItem
    
    Set oPurchaseOrder = New CAPPurchaseOrder
    
    oPurchaseOrder.Init FE_Application.SessionContext
    oPurchaseOrder.Load 13
    
    Set oLineItems = oPurchaseOrder.LineItems
    
    For Each oLineItem In oLineItems
    ''If line item is not posted then delete it.
        If oLineItem.Fields(APLINEITEMS_fld_POSTSTATUS) = "Not yet posted" Then
            MsgBox "Deleting line item: " & oLineItem.Fields(APLINEITEMS_fld_DESCRIPTION)
            oLineItems.Remove oLineItem
            oPurchaseOrder.Save
        End If
    Next oLineItem
  
   oPurchaseOrder.CloseDown
   Set oPurchaseOrder = Nothing
   Set oLineItem = Nothing
   Set oLineItems = Nothing
   
End Sub

Example of deleting a specific line item id (POLineItemID) :
''In this example, the line item id = 39
For Each oLineItem In oLineItems
    count = count + 1
        If oLineItems.Item(count).Fields(APLINEITEMS_fld_AP7LINEITEMSID) = 39 Then
          oLineItems.Remove oLineItem
          oPurchaseOrder.Save
        End If
    Next oLineItem

Disclaimer: We provide programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes you are familiar with Microsoft Visual Basic and the tools used to create and debug procedures. Our Customer Support can help explain the functionality of a particular procedure, but we will not modify, or assist you with modifying, these examples to provide additional functionality. If you are interested in learning more about the VBA and API modules, contact your account manager.