Home › Forums › Add-ons › Repeater Field › How do I programmatically delete all repeater rows? › Reply To: How do I programmatically delete all repeater rows?
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;
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.