Could you please check the OOTB pzStudioContainer section. In that you have a drop down menu which has a close all option. Please hit the Close all button at runtime and trace the activity using the tracer. This can help you to give idea to create a button and call the OOTB close all activity
To make sure you are not firing the onclick on unnecessary spans or unwanted spans, make sure the spans are all inside the div tabcontainer. Once you find these spans, just do click action. This way, you do an ootb way of closing the workarea tabs and the harness resize etc should trigger automatically.
var l = document.getElementById('close');for(var i=0; i<50; i++)