Share via


SPListItemCollection.Delete method

Deletes the item at the specified index in the collection.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Sub Delete ( _
    iIndex As Integer _
)
'Usage
Dim instance As SPListItemCollection
Dim iIndex As Integer

instance.Delete(iIndex)
public void Delete(
    int iIndex
)

Parameters

  • iIndex
    Type: System.Int32

    A 32-bit integer that specifies the index.

Remarks

The Delete method deletes an item based on its index in the collection. To delete an item based on its ID, use the DeleteItemById method.

This method returns an ArgumentOutOfRangeException exception if the specified index is outside the valid range of indices for the collection.

Examples

The following code example deletes any items from the specified list in which an integer field value is less than 70 or a text field value equals None.

The For loop in the example counts downward (intindex-- ) instead of upward (intindex++ ) because items are being deleted and the number of items decreases with each increment.

Dim site As SPWeb = SPControl.GetContextWeb(Context)
Dim srcList As SPList = site.Lists("List_Name")
Dim listItems As SPListItemCollection = srcList.Items
Dim intIndex As Integer

For intIndex = listItems.Count - 1 To 0 Step -1

    If Convert.ToInt32(listItems(intIndex)("Field1_Name")) < 70 _ 
        OrElse listItems(intIndex)("Field2_Name").ToString() = "None" Then

        listItems.Delete(intIndex)

    End If

Next i
SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["List_Name"];
SPListItemCollection collListItems = oList.Items;

for (int intIndex = collListItems.Count - 1; intIndex > -1; intIndex--)
{
    if (Convert.ToInt32(collListItems[intIndex]["Field1_Name"]) < 70 ||
        collListItems[intIndex]["Field2_Name"].ToString() == "None")
    {
        collListItems.Delete(intIndex);
    }
}

See also

Reference

SPListItemCollection class

SPListItemCollection members

Microsoft.SharePoint namespace