#RTCNA Wish 5: Delete (almost) all revisions

Scott sent a tweet about deleting all revisions from a project. The tricky part is that, just like how the UI grays out the Delete button when there is only one revision in the project, we can’t use the API to delete the last revision.
Capture
This code uses the Revit 2015 API because the Revision class is new to 2015.

public void DeleteRevisions()
{
    Document doc = this.ActiveUIDocument.Document;

    // get list of all revisions in the document
    ICollection<ElementId> elementIds = new FilteredElementCollector(doc)
        .OfClass(typeof(Revision))
        .ToElementIds();
    
    // Remove the first revision from the list
    // Revit must have one revision in the document, so we can't delete them all
    elementIds.Remove(elementIds.First());

    using (Transaction t = new Transaction(doc,"Delete Revisions"))
    {
        t.Start();
        doc.Delete(elementIds);
        t.Commit();
    }
}

3 thoughts on “#RTCNA Wish 5: Delete (almost) all revisions

  1. Awesome! Thank you, looks relatively straightforward! Now to try and find the customer at #RTCNA that asked me if it could be done!

  2. Hi,

    This command works great, but I was trying to modify this routine so that it would delete all but the first sequence number not the first created revision but I keep getting an internal exception

    “a managed exception was thrown by revit or by one of its external applications”

    My code is below, am I going in the right direction?

    public void Allbutfirstrevdelete()
    {
    Document doc = this.ActiveUIDocument.Document;
    try
    {
    // Find revisions whose description matches input string
    ICollection elementIds = new FilteredElementCollector(doc).OfClass(typeof(Revisions)).ToElementIds();

    // Check if revision should be added
    foreach (ElementId revision in elementIds)
    {
    Element e = doc.GetElement(revision);
    Parameter revsequence = e.get_Parameter(BuiltInParameter.PROJECT_REVISION_SEQUENCE_NUM);
    string sequence = revsequence.AsString();
    if (sequence==”1″)
    elementIds.Remove(e.Id);
    }

    if (elementIds.Count > 0)
    {
    // Apply the new list of revisions
    Transaction t = new Transaction(doc, “Add revisions to sheet”);
    t.Start();
    doc.Delete(elementIds);
    t.Commit();

    }
    }
    catch(Exception x)
    {
    TaskDialog.Show(“ddd”,x.Message);
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s