Deleting elements using Python

Hello guys, i’m a very beginner at python programing, i’m trying to get some walls deleted with revit API in python but got an issue !
the Python shell tells me “expected element got wall” but i’m using all ELEMENTS of category !! Please some help

The delete method expects element ids, not the actual element :slight_smile:

2 Likes

@Martin_Spence1 thank you !! it worked fine :slight_smile:

Sweet :+1::slight_smile:

Hi, so the Element.ID node gives the Revit element ID as a string, how can that be fed into the Python script? Thanks

[Edit Moderation : post edited to avoid confusion ElementId() method does not accept Element as parameter ]

worked it out, just need to add:

ID = Autodesk.Revit.DB.ElementId(walls)

before

doc.Delete(walls)

@egdivad Is it possible to share a screenshot of you code? I know I can use a springs node, but I am trying to replicate your end result in python and am getting the error: “Autodesk is not defined”

Here is a copy of my code:
import clr

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference(“RevitNodes”)

doc = DocumentManager.Instance.CurrentDBDocument

viewstodel = UnwrapElement (IN[0])
deleted =

TransactionManager.Instance.EnsureInTransaction(doc)
for views in viewstodel:
ID = Autodesk.Revit.ElementID(views)
doc.Delete(views)
deleted.append(views)

TransactionManager.Instance.TransactionTaskDone()

out = deleted

I am clearly missing something, I just don’t know what.

You’re missing required imports. Check the information on this page for more info: 3.2 Boilerplate Setup Code - Dynamo Python Primer

Note that this isn’t EVERY namespace and class you’ll need, but it covers most of the basics.

1 Like

That’s a super helpful resource @jacob.small . This solved my initial error but now gives a new one: 'unexpected token " '. I don’t notice any irregularities or extra ’ " 's anywhere.

Here is a grab of the Code:

Any ideas?

Manually retyping the whole code now instead gives the error:
“File “string”, line 36, in AttributeError:attribute ’ ElementID’ of ‘namespace#’ object is read-only”

so its clearly my Element ID line, I just don’t know why.

Since you manually retyped it, we’d have to see what you typed. Otherwise we’re guessing at what you might have retyped wrong. Might be that you’re trying to pull an element ID from an element ID when you really want to convert the ID to an element.

You may also find it’s easier to use a node to do this instead of Python. There is one out of the box in Revit 2021 and on, and one in Archi-Lab, Springs, and others.

Here is a screen grab of the code after retyping.


I suspect the issue is the latter of what you suggested.

I know I can do it with packages, but want to accomplish this OOTB for 2019.

Please post the DYN - too much guesswork here and me retyping your code is slowing us both down.

Sorry if I was making things difficult. Here is the dyn:
Dynamo-StripAllNonWorkingViews-OOTB-R19.dyn (22.0 KB)

@GJax89
According to the Revit API doc

the syntax for delete one Element

import clr
import sys
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

element = UnwrapElement(IN[0])
element_Id = element.Id
TransactionManager.Instance.EnsureInTransaction(doc)
doc.Delete(element_Id)
TransactionManager.Instance.TransactionTaskDone()

OUT = element_Id

the syntax for delete several Elements

import clr
import sys
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

from System.Collections.Generic import List

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

toList = lambda x : x if hasattr(x, '__iter__') else [x]

lst_elements = toList(UnwrapElement(IN[0]))
lst_elementIds = List[ElementId]([x.Id for x in lst_elements])

TransactionManager.Instance.EnsureInTransaction(doc)
doc.Delete(lst_elementIds)
TransactionManager.Instance.TransactionTaskDone()

OUT = lst_elementIds
5 Likes

Thank you for helping with the syntax, I wasn’t aware there were different syntax for single v multiple elements. If I copy your code directly, I get a new error for line 22 of your code: List[object] object has no attribute ‘Id’. I am clearly missing something, but admittedly haven’t had time to dig very deeply at all into either python or the Revit API.

if your input data is a list (multiple items) you should use the second example

I have, that’s where the error is coming in.

What is the entry structure of your list? Several nested list ?

Its a single list of Ids, I have tried as a normal list, output from the node ‘Element.Ids’, converting that node to string from object, and using list flatten on all above methods. Here is a screen grab:


Edited to get node names visible in grab. Apologies