Many insect species lay their eggs according to a clumped distribution, which causes food shortage among the larvae. To avoid starvation, at least some larvae have to migrate to another plant at some stage. Even though this migration involves a high mortality risk, larvae (of, for example, the cinnabar moth) often leave before their host plant is defoliated, thereby enabling others to remain safely on the plant. To understand why this remarkable behaviour has evolved, we modelled the situation of two larvae on a small plant as a war of attrition. Our main conclusions are that, in the case where the larvae are unrelated, they should only migrate prior to defoliation in certain time periods, called migration periods, when the pay-off of leaving decreases. Moreover, the optimal migration time is a random variable. When the pay-off of leaving fluctuates, there can be several migration periods. In the case where the larvae are related, it can also be optimal to migrate when the pay-off of leaving increases. Relatedness increases the length and the number of the migration periods, as well as the leaving tendency during these periods. Furthermore, relatedness gives rise to a parent-offspring conflict over the optimal migration strategy.