When using a loop method to delete the rows one at a time, always delete the last row each time, not the first.
You won’t notice it on small repeaters, but on larger ones it can really bite.
I used the delete first row method (as is commonly shown online, I think coz it’s easier) and for 376 rows in a repeater, it took 5244 seconds. Deleting the last row each time, it took 191 seconds.
I do still want to find a faster way. But that was tonight’s optimisation
Here’s what my code looks like:
function deleteRows(string $acfRepeaterFieldKey, int $postID): void {
reset_rows();
$fieldValue = get_field($acfRepeaterFieldKey, $postID);
if (is_array($fieldValue)){
$remainingRows = count($fieldValue);
while (have_rows($acfRepeaterFieldKey, $postID)) :
the_row();
delete_row($acfRepeaterFieldKey, $remainingRows--, $postID);
endwhile;
}
}
Awesome, thanks, John. Will look into that.