'How to deal with boxes repainting pine script

The pine script draw several boxes and lines when barstate.islast. Each box is calculated base on a length and other condition. enter image description here

The problem is that, it will replaint the same boxes because of barstate.islast. Is there a way only draw the boxes once or delete all the previous boxes?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source