Alright, here's an example that's worked on my assemblies so far, the only concern is if you have individual members of a pattern invisible or not, those won't get deleted since you can't delete individual pattern members. If you think there are entire patterns that could be invisible in every view and need to be deleted, let me know and I'll figure out how to add that to the code.
Other than that, here's the example:
objApp = ThisApplication
objTN = objApp.TransactionManager.StartTransaction(ThisDoc.Document, "Delete Unused Parts")
objApp.ScreenUpdating = False
objApp.SilentOperation = True
objDef = ThisDoc.Document.ComponentDefinition
Dim objComp As ComponentOccurrence
colOccs = objDef.Occurrences
Dim lisOccs As New ArrayList
For Each objComp In colOccs
lisOccs.Add(objComp)
Next
Dim objView As DesignViewRepresentation
colViews = objDef.RepresentationsManager.DesignViewRepresentations
On Error Resume Next
i = 1
Dim intViewCount As Long = colViews.Count - 1
objProgBar = objApp.CreateProgressBar(False, intViewCount, "Delete Unused Parts")
For Each objView In colViews
If objView.Name <> "Master" Then
objProgBar.Message = "Scanning " + objView.Name + " for Unused Parts. (View " + CStr(i) + " of " + CStr(intViewCount) + ")"
objProgBar.UpdateProgress
objView.Activate
For Each objComp In colOccs
If Component.Visible(objComp.Name) = True Then lisOccs.Remove(objComp)
Next
i += 1
End If
Next
objProgBar.Close
For Each objComp In lisOccs
objComp.Delete
Next
objApp.ScreenUpdating = True
objApp.SilentOperation = False
InventorVb.DocumentUpdate()
objTN.End
Screen Updating is turned off so the computer doesn't waste time generating each view as it gets activated, this speeds up the code significantly. Silent Operation is turned to eliminate any pop-ups the regular Inventor software uses, if there are any add-ins like the Vault that are still popping up, let me know and I'll see if it's possible to deactivate those add-ins, the Vault can be turned on and off without issue, others I'm not so sure. Basically a list of all Occurrences (Parts and Assemblies) in the document is created. Then for each view rep, if an occurrence is visible, it's removed from the list, leaving you with a list of parts that gets deleted at the end.
While looking through each view rep, there's a progress bar that will pop up informing you of the progress so far. The whole code is also wrapped in a single transaction in the change processor. So if the code does something you don't want, just click Undo once and it will take you back to before the code was run.