LearnRevitAPI
![](https://framerusercontent.com/images/zMFB840Y52OLteloUT4BRuogE.png)
![](https://framerusercontent.com/images/U9zjOvD4k3m6cIJd5bOiu960DeI.png)
Jul 2, 2024
Purge View Templates
![](https://framerusercontent.com/images/Sd5FTaxiqzG7oeH1ykV6yypUzXo.png?scale-down-to=512)
I'm continuing with the Purge Tools for Revit series, and this time we will look at how to purge unused View Templates.
This tool will also be included in EF-Tools, but I will also explain all the steps in the code.
💡Let's Learn: How To use Purge View Templates with pyRevit
![](https://framerusercontent.com/images/6qnGDYRClBYkuABoB5mGje8luw.png)
You can use it in EF-Tools
Before we dive into the code, I want you to know that you can use this tool in EF-Tools.
Inspect the code by holding ALT and clicking on the button to open the location of the source code.
![](https://framerusercontent.com/images/9SVj6SYNG1OJkFkA0TcO22C5c.png)
👀Now let's look at the code.
1️⃣ Get View and ViewTemplates
First of all, you will need to get your Views and View Templates.
It's also a good idea to sort them accordingly.
![](https://framerusercontent.com/images/QxM15v2k7FFCO0E1JyTItqAhdOE.png)
Get Used ViewTemplates
Now, we can iterate through all views and get ViewTemplateId.
Then, it's important to check that it's not ElementId(-1), because it refers to None in Revit API.
![](https://framerusercontent.com/images/EtcXXju5laUPbhNB3vUFPTt180U.png)
Get Unused ViewTemplates
We have all view templates, and we have a list of used view templates.
Now we can turn them into sets and subtract one from another to get the difference, which will be our unused View Templates.
💡 It's also a good idea to ensure we have unused templates.
![](https://framerusercontent.com/images/z47iV2y3B3vZNh3Jw97Wh457CM.png)
Select ViewTemplates to Purge
Now it's time to give some control to our users.
We shouldn't delete all unused view templates; instead, we should ask them to select which ones to delete.
We can use pyrevit.forms.SelectFromList to quickly create a list of our unused View Templates. 💡
Make sure the user has selected something here.
![](https://framerusercontent.com/images/68RspxSU9vW0MjhyDxYB7iN1E.png)
Purge ViewTemplates
Finally, we can get rid of these unused view templates.
Overall, we just need to use doc.Delete method, but it's also good to add try/except in case these templates are used by other users or something else.
![](https://framerusercontent.com/images/j0uWxwK8oE0auO2rw9KCGuitjw.png)
Full Code
👇Here is the final code with all the steps together.
![](https://framerusercontent.com/images/NSUttx1D9wBPDucJlXWwO8tjTs.png)
💡You can copy code snippets in this blog post.
P.S. Want to Purge View Filters too?
I've forgotten to send out the newsletter about the first purge tool - Purge View Filters.
You can check this video here if you want to learn about it too.
Join Newsletter
📩 You will be added to Revit API Newsletter
Join Us!
which is already read by 6700+ people!
![](https://framerusercontent.com/images/svHzzMLtnnRCxtCUwbgAm6CUeB8.jpg)
![](https://framerusercontent.com/images/Y44kwhY0i7q37LzXUjrlxeGUo.jpg)
![](https://framerusercontent.com/images/lqKYmh8rqwZwgY2C9MSQER6iBCI.jpg)
![](https://framerusercontent.com/images/SyGCvJ3dbHsJEyklWWBhweY.jpg)
![](https://framerusercontent.com/images/DwiaUdmQHs3FVdDH2PXwAr0Y.jpg)