Use dynamo to Delete All View Templates

What do you mean by {“Is there a way then to remove all templates from my views then delete all templates not in use”}

What is your end goal? Delete all view templates or delete which one not in use?

To collect all view templates do this:

List<View> vtList = new FilteredElementCollector(DocumentManager.Instance.CurrentDBDocument)
                .OfClass(typeof(View))
                .Cast<View>()
                .Where(x => x.IsTemplate)
                .ToList();

Then to delete it you will have to make a list of their Ids and do this:

doc.Delete(e.Id);

Once you delete all view templates I don’t see how they would need to be removed from a view. There no longer will be anything applied to views. It doesn’t mean that your visibility settings will reset. They will stay the same as they were based on last view template applied. This behavior is the same with UI.

1 Like

To delete all templates.

Thanks Konrad.

Just to check, the script, where do I insert this. I’m new to dynamo and have really only done the visual programming part. Plugging the code in will work too, I just need to know where to put it. I appreciate the help.

Ahh, in that case just run this code. I assumed you would know how to translate that to Python or ZeroTouch node. My bad.

# Copyright(c) 2016, Konrad K Sobon
# @arch_laboratory, http://archi-lab.net

# Import DocumentManager and TransactionManager
import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

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

import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

#The inputs to this node will be stored as a list in the IN variable.
dataEnteringNode = IN

if IN[0]:
	try:
		errorReport = None
		TransactionManager.Instance.EnsureInTransaction(doc)
		
		allViewTemp = FilteredElementCollector(doc).OfClass(View).ToElements()
		toDelete = []
		for i in allViewTemp:
			if i.IsTemplate:
				try:
					doc.Delete(i.Id)
				except:
					pass

                TransactionManager.Instance.TransactionTaskDone()
	except:
		# if error accurs anywhere in the process catch it
		import traceback
		errorReport = traceback.format_exc()
else:
	errorReport = "Set input to True"
#Assign your output to the OUT variable
if errorReport == None:
	OUT = 0
else:
	OUT = errorReport

4 Likes

Thanks @Konrad_K_Sobon. I ran it, however I did get a response:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
File “”, line 34
TransactionManager.Instance.TransactionTaskDone()

     ^

SyntaxError: unexpected token ‘TransactionManager’

Ahhh the formatting got a little screwed up when I pasted it. Make sure that TransactionManager.Instance.EnsureInTransaction(doc) has the same indentation as TransactionManager.Instance.TransactionTaskDone() so hit TAB twice for the latter.

Hopefully this is the final problem. I made sure I deleted the indentation on both, then put 2 tabs in on both lines. But got the following… @Konrad_K_Sobon you’re a great help!!!

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
File “”, line 38
TransactionManager.Instance.TransactionTaskDone()

                                                   ^

SyntaxError: unexpected token ‘’

no, the idea is that they are in the same line together like this:

1 Like

I think that is what I did. Tell me if im missing something.

looks good now make sure they are TABS and not SPACES. Python is sensitive about this.

I’ve done eveything as you’ve said. Still no luck. I’ll try again on monday. Thanks again.

just take this. viewTemp.dyn (3.0 KB)

2 Likes

It’s best to delete the templates in code as Konrad suggested because currently 3d view templates can not be wrapped to a Dynamo object. Tho if you don’t mind deleting said 3d templates manually, you can use the following nodes as an alternative:

4 Likes

Worked like a charm. I’ll check where mine went wrong the first time. You’re a legend. Thanks for the help.

Great Job…Again

Both methods work fine. However would it be possible to only delete the templates not in use?

Cheers.

Hi Konrad

Do you have a script to Delete Unused Templates?

Thanking you in advance

Rob

@Rob_Cross please start a new thread. This one is old, and has been answered.

1 Like