In Pinescript 6, if you want both to remove the box from the array AND delete the drawn box from the chart, you should implement in the last if scope:
(boxtop.get(i)).delete()
array.remove(boxTop, i) //or boxTop.remove(i)
(boxes.get(i)).delete()
array.remove(boxes, i) //or boxes.remove(i)