Delete Method (ElementId)


Deletes an element from the document given the id of that element.

Namespace: Autodesk.Revit.DB
Assembly: RevitAPI (in RevitAPI.dll) Version: 21.0.0.0 (21.1.1.109)

Syntax

C#
public ICollection<ElementId> Delete(
	ElementId elementId
)
Visual Basic
Public Function Delete ( _
	elementId As ElementId _
) As ICollection(Of ElementId)
Visual C++
public:
ICollection<ElementId^>^ Delete(
	ElementId^ elementId
)

Parameters

elementId
Type: Autodesk.Revit.DB ElementId
Id of the element to delete.

Return Value

The deleted element id set.

Remarks

This method will delete the element 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.

Examples

Copy C#
private void DeleteElement(Autodesk.Revit.DB.Document document, Element element)
{
    // Delete a selected element.
   ICollection<Autodesk.Revit.DB.ElementId> deletedIdSet = document.Delete(element.Id);

    if (0 == deletedIdSet.Count)
    {
        throw new Exception("Deleting the selected element in Revit failed.");
    }

    String prompt = "The selected element has been removed and ";
    prompt += deletedIdSet.Count - 1;
    prompt += " more dependent elements have also been removed.";

    // Give the user some information
    TaskDialog.Show("Revit",prompt);

}
Copy C#
private void DeleteElement(Autodesk.Revit.DB.Document document, Element element)
{
    // Delete an element via its id
    Autodesk.Revit.DB.ElementId elementId = element.Id;
    ICollection<Autodesk.Revit.DB.ElementId> deletedIdSet = document.Delete(elementId);

    if (0 == deletedIdSet.Count)
    {
        throw new Exception("Deleting the selected element in Revit failed.");
    }

    String prompt = "The selected element has been removed and ";
    prompt += deletedIdSet.Count - 1;
    prompt += " more dependent elements have also been removed.";

    // Give the user some information
    TaskDialog.Show("Revit", prompt);
}
Copy VB.NET
Private Sub DeleteElement(document As Autodesk.Revit.DB.Document, element As Element)
    ' Delete a selected element.
    Dim deletedIdSet As ICollection(Of Autodesk.Revit.DB.ElementId) = document.Delete(element.Id)

    If 0 = deletedIdSet.Count Then
        Throw New Exception("Deleting the selected element in Revit failed.")
    End If

    Dim prompt As [String] = "The selected element has been removed and "
    prompt += deletedIdSet.Count - 1
    prompt += " more dependent elements have also been removed."

    ' Give the user some information
    TaskDialog.Show("Revit", prompt)

End Sub
Copy VB.NET
Private Sub DeleteElement(document As Autodesk.Revit.DB.Document, element As Element)
    ' Delete an element via its id
    Dim elementId As Autodesk.Revit.DB.ElementId = element.Id
    Dim deletedIdSet As ICollection(Of Autodesk.Revit.DB.ElementId) = document.Delete(elementId)

    If 0 = deletedIdSet.Count Then
        Throw New Exception("Deleting the selected element in Revit failed.")
    End If

    Dim prompt As [String] = "The selected element has been removed and "
    prompt += deletedIdSet.Count - 1
    prompt += " more dependent elements have also been removed."

    ' Give the user some information
    TaskDialog.Show("Revit", prompt)
End Sub

Exceptions

Exception Condition
Autodesk.Revit.Exceptions ArgumentException The element elementId does not exist in the document -or- ElementId cannot be deleted.
Autodesk.Revit.Exceptions ArgumentNullException A non-optional argument was NULL
Autodesk.Revit.Exceptions 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.
Autodesk.Revit.Exceptions ModificationOutsideTransactionException The document has no open transaction.

See Also