Well here I am again. These while loops might help you out a bit /clearing...
var %x, %y, %z, %a ;/clearing all channels... set %x 1 while ($chan(%x)) { set %y $ifmatch clear %y } inc %x ;/clearing all queries... set %x 1 while ($query(%x)) { set %z $ifmatch clear %z } inc %x