Thought I share this script I just made. I was trying to delete views from my project browser and kept getting “Cannot delete pinned elements”. Quite annoying even though the views were not on sheets. I found this on the forums. Not sure if this is a result from also using the pyRevit “Pin All Viewports” tool.
simply select one or more views from the Project Browser
# -*- coding: utf-8 -*-
import clr
clr.AddReference("RevitAPI")
clr.AddReference("RevitServices")
from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
from pyrevit import revit, script
doc = revit.doc
uidoc = revit.uidoc
selection = uidoc.Selection.GetElementIds()
if not selection:
script.show.warning("No views selected. Please select views in the Project Browser.")
script.exit()
# Start transaction
t = Transaction(doc, "Unpin Selected Views")
t.Start()
unpinned = []
skipped = []
for eid in selection:
el = doc.GetElement(eid)
if isinstance(el, View):
try:
if el.Pinned:
el.Pinned = False
unpinned.append(el.Name)
else:
skipped.append(el.Name)
except Exception as e:
skipped.append(el.Name + " (Error)")
else:
skipped.append(str(el))
t.Commit()
# Report
output = script.get_output()
output.print_md("### 🔓 Unpin Results")
if unpinned:
output.print_md("**✅ Unpinned Views:**")
for name in unpinned:
output.print_md("- `{}`".format(name))
else:
output.print_md("*No views were unpinned.*")
if skipped:
output.print_md("\n**⚠️ Skipped or Already Unpinned:**")
for name in skipped:
output.print_md("- `{}`".format(name))
```