DocumentDelete(ICollectionElementId) Method | 
   Deletes a set of elements from the document.
Namespace: Autodesk.Revit.DBAssembly: RevitAPI (in RevitAPI.dll) Version: 25.0.0.0 (25.0.0.0)
Syntaxpublic ICollection<ElementId> Delete(
	ICollection<ElementId> elementIds
)
Public Function Delete ( 
	elementIds As ICollection(Of ElementId)
) As ICollection(Of ElementId)
public:
ICollection<ElementId^>^ Delete(
	ICollection<ElementId^>^ elementIds
)
member Delete : 
        elementIds : ICollection<ElementId> -> ICollection<ElementId> Parameters
- elementIds  ICollectionElementId
 - 
   The ids of the elements to delete.
 
Return Value
ICollectionElementId
   The deleted element id set.
Exceptions| Exception | Condition | 
|---|
| ArgumentException | 
   One or more elements in elementIds do not exist in the document.
   -or-
   One or more of the elementIds cannot be deleted.
 | 
| ArgumentNullException | 
   A non-optional argument was null
 | 
| ModificationForbiddenException | 
   The document is in failure mode: an operation has failed,
   and Revit requires the user to either cancel the operation
   or fix the problem (usually by deleting certain elements).
   -or-
   The document is being loaded, or is in the midst of another
   sensitive process.
 | 
| ModificationOutsideTransactionException | 
   The document has no open transaction.
 | 
Remarks
   This method will delete the elements and any elements that are totally dependent upon that element. Any references to the deleted elements will become invalid and hence cause an exception to be thrown if they are accessed.
   The elements will be deleted with no prompts for user confirmation.  Pinned elements will be deleted with no warnings.
   
Note: in a family document, the predefined elements (those elements inherited from its family template file) can't be deleted by this method.
Example
ICollection<Autodesk.Revit.DB.ElementId> idSelection = null ;
 UIDocument uidoc = new UIDocument(document);
 ICollection<ElementId> selectedIds = uidoc.Selection.GetElementIds();
 foreach (ElementId id in selectedIds)
 {
     idSelection.Add(id);
 }
 ICollection<Autodesk.Revit.DB.ElementId> deletedIdSet = document.Delete(idSelection);
 if (0 == deletedIdSet.Count)
 {
     throw new Exception("Deleting the selected elements in Revit failed.");
 }
 TaskDialog.Show("Revit","The selected element has been removed.");
Dim idSelection As ICollection(Of Autodesk.Revit.DB.ElementId) = Nothing
Dim uidoc As New UIDocument(document)
Dim selectedIds As ICollection(Of ElementId) = uidoc.Selection.GetElementIds()
For Each id As ElementId In selectedIds
    idSelection.Add(id)
Next
Dim deletedIdSet As ICollection(Of Autodesk.Revit.DB.ElementId) = document.Delete(idSelection)
If 0 = deletedIdSet.Count Then
    Throw New Exception("Deleting the selected elements in Revit failed.")
End If
TaskDialog.Show("Revit", "The selected element has been removed.")No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
UIDocument uidoc = new UIDocument(document); 
ICollection<ElementId> elements = uidoc.Selection.GetElementIds();
ICollection<Autodesk.Revit.DB.ElementId> deletedIdSet = document.Delete(elements);
if (0 == deletedIdSet.Count)
{
    throw new Exception("Deleting the selected elements in Revit failed.");
}
TaskDialog.Show("Revit","The selected element has been removed.");
Dim uidoc As New UIDocument(document)
Dim elements As ICollection(Of ElementId) = uidoc.Selection.GetElementIds()
Dim deletedIdSet As ICollection(Of Autodesk.Revit.DB.ElementId) = document.Delete(elements)
If 0 = deletedIdSet.Count Then
    Throw New Exception("Deleting the selected elements in Revit failed.")
End If
TaskDialog.Show("Revit", "The selected element has been removed.")No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
See Also