Delete RVT links entirely from Revit file

I am trying to create a Dynamo script that will open a Revit file (in the background), remove all Revit links from that file, and then save over that current Revit file.

I can find all link types & instances and delete them, but they remain in the Manage Links browser, and thus still need to be temporarily upgraded whenever the host files are upgraded.

I cannot find anything online that discusses removing Revit links (deleting the linked files entirely from the host file). Everything I find online discusses removing the elements or instances.

Is this something the Revit API can do?

Thanks in advance

Edit: I am still very new at Dynamo, and do not have any Python experience. So if Python is the solution, I will need a step by step on how to implement it

1 Like

This should help you get started: Remove RVT links - #11 by Ben_Barfoot

@Zack_C , @jacob.small ,

Hi,

I just come until here:

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

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

# The inputs to this node will be stored as a list in the IN variables.
linkInstances = FilteredElementCollector(doc).OfClass(RevitLinkInstance).ToElements()


OUT = linkInstances

So for deleting a linked file i need there ID but which one? Revit ID, unique Id, TypeID?

how do i remove it via for loop, or can i “clean up” my variable to None ?
do i clear, delete or remove my links ?

Needs the Element ID. Both the instances and the link type will need to be deleted.

Also you’ll need to open the many documents and loop over those, not work in the current document.

1 Like

Did you have any luck with this? i am looking at doing the same thing. (open a revit file in the background, remove linked revit files, close file) it appears that you can only delete instance of linked file and not the linked file.
i would have thought a node similar to remove.link would exist

Have you tried the above?

just delete the element type of the revit link types not wanted. Get all elements of Type Revit Link and delete elements. if you have already a script getting the revit link instances, then get the types of those, and delete

1 Like

I have not had a chance to run the script yet, however from the looks of it, it appears to be removing the linked elements placed in the model - I am looking to remove the links from being loaded in the model all-together

I am looking for a way to take old models, strip them of any loaded link, so when I upgrade them to a newer version for pulling information from, Revit does not have to go through the process of upgrading every loaded link

As said before you need to delete the link types. I have a node in Crumple which returns all linked files and their types/instances. Iirc I put it under Revit>Collect in the latest build. If you delete those types that should remove the link entirely from the model (similar to removing a link from manage links).

1 Like

For that purpose this is overkill. Consider using etransmit to open the file, or opening with all linked worksets closed.

1 Like

Thank you for your package.
Do you mind to share an example of what should be connected to the node?
DD#1012-F1-1.2 Crumple.Collect Revit Links

Generally this should collect any revit links as well as their placed instances. Havent seen any case where it didnt yet.

This is the node contents in Python for your testing if it helps, but looks to work for me currently. Wondering if maybe there’s something to do with BIM360 links involved.

Link types and instances.dyn (9.5 KB)

# Boilerplate text
import clr

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Persistence import DocumentManager

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

# Current doc/app/ui
doc = DocumentManager.Instance.CurrentDBDocument

# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

coll = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_RvtLinks).WhereElementIsNotElementType().ToElements()

insts, types = [], []

for c in coll:
	insts.append(c.ToDSType(True))
	types.append(doc.GetElement(c.GetTypeId()))

OUT = [insts,types]
1 Like

Hi Gavin, I’ve also been trying to collect all link types and remove them from my project. When I use your node and connect it to Element.Delete, the links disappear ( even though in the node results some of the entries are “false”)

However If I try to do this in a Python node

i get the following error:

image

Have you got an idea what I am doing wrong?

That list represents the type of each instance in the second output, so if you have links with more than one instance, only the first type will need to be deleted as by the time the next one is going to be the type is already gone.

2 Likes

Thank you for the reply. Currently I have 15 Revit links loaded in and there are 21 unique link types picked up by the node. This means that I can’t list unique items to avoid the deletion of the same type twice. I tried to cycle through the list in python to check whether each of the elements exist at all before it gets deleted, but this didn’t solve my issue and returned the same error, as I guess with the unique Ids they are picked up as unique entities, even thought some of them are not…?

Sorry for the double post, I just realised the 6 additional types are coming from links within other links.
image
Even when I filter them out from the deletion process I still get the same error as before.
What I don’t understand is why if I append all of the Non-nested links to a list and connect it to Elements.Delete it works brilliantly well and when I try to do it within Python it is so unhappy.

The Delete node likely has a builtin attempt to delete and if it fails it continues and returns a Null. You could achieve this in python using try/except.

1 Like

Thank you very much this resolved it!

1 Like