How to call the "PurgeUnused" Dynamo node in a Python Script?

Hi everyone,

I’m trying to use the new “PurgeUnused” node inside a Python script in Dynamo. Since the Revit API still does not have a direct method for this, is there a way to access the Dynamo node?

Thanks!

Dynamo only has access to what’s in the API. The method for collecting unused elements ( GetUnusedElements Method) was added in 2024. If you’re not in 2024 or later then you’d have to do this “manually” by determining which elements are “in use” and which are not.

In general, you can use Dynamo nodes by importing the appropriate library into python. For Revit nodes especially, you’re usually better off just using the API directly though.

EDIT: The primer goes more in detail and includes an example with the Revit Nodes library included as well.
Python and Revit | The Dynamo Primer

2 Likes

Alternatively, you can use the eTransmitForRevitDB library

Thank you both for your replies. They were really helpful.

I wasn’t aware of the GetUnusedElements method. It’s exactly what I needed. It makes sense that it’s available in the API since, as you mentioned, the Dynamo nodes rely on it.

However, I’ve encountered a problem when trying to delete AppearanceAssetElement objects. Despite trying different methods, including starting an AppearanceAssetEditScope, I haven’t been able to delete them. Do you have any suggestions?

Thanks again for your support!

After some research, I came across this article, which was incredibly helpful:
Purging Material Assets Using the Revit API the Right Way.

I implemented the intermediate solution, where AppearanceAssetElements retrieved via GetUnusedElements are compared with those collected using FilteredElementCollector. While functional, the article suggests this may not be the best approach. This made me curious about how to invoke GetUnusedElements using System.Reflection. I attempted to translate the provided C# code into Python but wasn’t successful—I suspect my misunderstanding of System.Reflection is the issue.

For now, the script works fine, but if anyone has time to explain System.Reflection in this context, I’d greatly appreciate it. If not, I completely understand—I’ll revisit this topic as I learn more.

Lastly, for anyone facing a similar challenge, here are a few key pieces of code to implement:

  1. Revit Version: I am using Revit 2025.4.
  2. Retrieve all unused elements:
unused_elems = doc.GetUnusedElements(System.Collections.Generic.HashSet[ElementId]([ElementId(bic) for bic in System.Enum.GetValues[BuiltInCategory]()]))
  1. Get the document’s AppearanceAssetElements (which differ from those returned by GetUnusedElements):
mat_asset = FilteredElementCollector(doc).OfClass(AppearanceAssetElement).ToElements()

Thanks in advance for any insights!

Edit: Spoke too soon. The script was working in a project but I still can’t delete AppearanceAssetElements from purged materials inside a family.

1 Like