Element.Delete View templates works but still produces warning

I have a project startup script that removes certain templates views extra from new projects. I’ve ran it and the node Element.Delete has removed the correct view templates but is still sending a warning.

Warning: Element.Delete operation failed.
The referenced object is not valid, possibly because it has been deleted from the database, or its creation was undone.

This is the only instance of Element.Delete in the script that is giving me this issue.

This is the section that removes the view template.

This section provides the FilterList for the STring.Contains node above.

This section creates the selection for the dictionary above.

Revit Dev - Project Startup.dyn (231.9 KB)

I’ve been working on fixing another part of the script and this issue has gone away. I’m not sure how. The only thing I changed was at a distant area of the chart. I simply changed the categories pullown that provides the category to “All Elements of Category.” I can’t understand how that would change it.

If you’re running in Automatic or running multiple times while testing the script it could just be Dynamo trying to delete what has already been deleted.

You mentioned that you have other instances of the Delete node elsewhere that are working properly. What else are you deleting? Once of these nodes could be deleting a view template.

The closest thing I have another delete node deleting is Views. Since I’ve changed it I’ve ran the script probably 10 times and the issue hasn’t occurred again. I’m using manual to run it and because of the nature of the script I close the project without saving and reopen it.

Be careful with your views. View Templates are View objects so you may be deleting some of the templates that way. You can filter out the templates with View.IsViewTemplate.

1 Like

Good point. I’ve added this to the view portion.

If you’re using the same logic to filter and delete the views as you are the view templates then you could even leave it the way you had it and remove the view template portion since all elements of the view category would already include your view templates.

Yeah, but I want to leave them separate because some of the values differ depending on discipline which is selected earlier.