Well, there's a few different ways you can do it, but I'd probably just go for the path of least reisistance and say 'if there is already something in the container, don't generate treasure'. To do that, I just added GetFirstItemInInventory() != OBJECT_INVALID to your script below.
#include "NW_O2_CONINCLUDE"
void main()
{
float fDelay = 30.0; //30 second delay
if (GetLocalInt(OBJECT_SELF,"NW_DO_ONCE") != 0 || GetFirstItemInInventory() != OBJECT_INVALID)
{
return;
}
object oLastOpener = GetLastOpener();
GenerateLowTreasure(oLastOpener, OBJECT_SELF);
SetLocalInt(OBJECT_SELF,"NW_DO_ONCE",1);
DelayCommand(fDelay,SetLocalInt(OBJECT_SELF,"NW_DO_ONCE",0));
ShoutDisturbed();
}
But if you really want to destroy everything in the container though and then generate treasure, put this code in right after the brackets that have the 'return;' in them:
object oInvItem = GetFirstItemInInventory();
while (oInvItem != OBJECT_INVALID) {
DestroyObject(oInvItem);
oInvItem = GetNextItemInInventory();
}