With some research I have come up with the solution but it comes with few limitations too. We can call the Revit buttons from the API, including Purge.
Limitations:
- You can't call a button from half way through your plugin.
- You have to set it up to run after your plugin is done.
- You might be able to get around this with subscribing to the Executed event but I'll need to experiment with that.
- You don't know whether the command will run successfully.
- Also, as you can't run it half way through your program, you have no way of checking whether it's succeeded within code.
- You can check the journal logs and parse the result of the latest command but that's a very hack-y feeling solution.
- You don't know ahead of time whether someone else has overridden the button to do something else.
- The call is to just press the button. The article above focuses on overriding buttons. You can't guarantee that the button still does what you're expecting it to.
- The process you trigger by pressing the button cannot be automated.
- Your script has to finish before the button is pressed so you can't take control of the resulting dialogs.
- You may be able to subscribe to the button events and fiddle something that way. Again, experimentation needed.
Following is the sample code to achieve this:
UIApplication uiapp = commandData.Application;
//Store the ID of desired plugin button, in this case its 'purge unused'
String s_commandToDisable = "ID_PURGE_UNUSED";
RevitCommandId s_commandId = RevitCommandId.LookupCommandId(s_commandToDisable);
//This revit button will run at the end of your application.
uiapp.PostCommand(s_commandId);
To find list of default revit command id, click here