How to delete selected features


This sample code deletes all selected features in the edit workspace. The sample works a little differently than the standard delete command, it will continue deleting features even if it detects features belonging to a feature class that is not in the edit session.
In many cases you can call IFeature::Delete to delete a feature. However, it is always safer to add the features to a Set object and call IFeatureEdit::DeleteSet. For example, if you delete a feature that has a related feature, the related feature may automatically be deleted; if, however, the related feature is also selected, when looping through the feature selection, you might try to delete a feature that has already been removed. Calling DeleteSet solves this problem.

How to use

  1. Select one or more editable features.
  2. Paste the code into VBA and run the marcro.
[VBA]
Public Sub DeleteSelectedFeatures()
    Dim pDeleteSet As ISet
    Dim pEditLayers As IEditLayers
    Dim pEditor As IEditor
    Dim pEnumLayer As IEnumLayer
    Dim pFeature As IFeature
    Dim pFeatureEdit As IFeatureEdit
    Dim pFeatureCursor As IFeatureCursor
    Dim pFeatureLayer As IFeatureLayer
    Dim pFeatureSelection As IFeatureSelection
    Dim pID As New UID
    Dim pInvalidArea As IInvalidArea
    Dim pSelectionSet As ISelectionSet
    Dim bInOperation As Boolean
    Dim DeletedFeatureCount As Integer
    Dim LayerCount As Integer
    
    On Error GoTo ErrorHandler
    
    ''Get a handle to the Editor extension
    pID = "esriEditor.Editor"
    Set pEditor = Application.FindExtensionByCLSID(pID)
    If Not pEditor.EditState = esriStateEditing Then Exit Sub
    Set pEditLayers = pEditor
    
    If pEditor.SelectionCount = 0 Then Exit Sub
    
    'Set up the InvalidArea object
    Set pInvalidArea = New InvalidArea
    Set pInvalidArea.Display = pEditor.Display
    
    'Loop through the selected features and delete them
    pEditor.StartOperation
    bInOperation = True
    
    pID = "{40A9E885-5533-11D0-98BE-00805F7CED21}" 'IGeoFeatureLayer
    Set pEnumLayer = pEditor.Map.Layers(pID, True)
    pEnumLayer.Reset
    Set pFeatureLayer = pEnumLayer.Next
    Do While Not pFeatureLayer Is Nothing
        'Check if feature layer is editable
        If pEditLayers.IsEditable(pFeatureLayer) Then
            Set pFeatureSelection = pFeatureLayer
            Set pSelectionSet = pFeatureSelection.SelectionSet
            ''Check if there is a selection
            If Not pSelectionSet.Count = 0 Then
                
                'Add each feature to a Set
                'and delete the set - need a Set incase there are related features
                Set pDeleteSet = New esriSystem.Set
                pSelectionSet.Search Nothing, False, pFeatureCursor
                Set pFeature = pFeatureCursor.NextFeature
                Do While Not pFeature Is Nothing
                    pInvalidArea.Add pFeature
                    pDeleteSet.Add pFeature
                    DeletedFeatureCount = DeletedFeatureCount + 1
                    Set pFeature = pFeatureCursor.NextFeature
                Loop
                pDeleteSet.Reset
                Set pFeatureEdit = pDeleteSet.Next
                Do While Not pFeatureEdit Is Nothing
                    pFeatureEdit.DeleteSet pDeleteSet
                    Set pFeatureEdit = pDeleteSet.Next
                Loop
                
            End If
        End If
        Set pFeatureLayer = pEnumLayer.Next
    Loop
    
    If DeletedFeatureCount = 0 Then
        pEditor.AbortOperation
    Else
        pEditor.StopOperation ("Delete")
        bInOperation = False
    End If
    
    'Clear the selection and refresh the display
    pEditor.Map.ClearSelection
    pInvalidArea.Invalidate esriAllScreenCaches
    
    Exit Sub
    
ErrorHandler:
    If bInOperation Then
        pEditor.AbortOperation
    End If
End Sub






Additional Requirements
  • An Edit Session.