Macro to roll back the design tree

I want to do a macro that roll back the design tree of a part before the first feature, insert a part, lock all references, delete the part inserted with the keep/delete body feature and then roll back the design tree after the last feature.

I'm new with Solidwork's macro and for now I'm able to insert the part and lock all the references, but I'm not able to manipulate the design tree and delete my part inserted with the keep/delete body feature. Can someone help me with that ?

Thanks

SolidworksApi macros