Home › Forums › ACF PRO › Pruning Orphaned Flex-field Data › Reply To: Pruning Orphaned Flex-field Data
Just saw this, here’s what I ended up with – it’s nearly identical to the above:
public static function remove_orphans($post_id) {
global $wpdb;
$wpdb->query($wpdb->prepare("
DELETE FROM {$wpdb->postmeta}
WHERE {$wpdb->postmeta}.post_id = %d
AND (
meta_key LIKE '".static::PAGE_BUILDER_ACF_FIELD."_%'
OR meta_key LIKE '_".static::PAGE_BUILDER_ACF_FIELD."_%'
)",
$post_id
));
wp_cache_flush();
}
I’ve now tested this pretty extensively, having re-saved all the pages on a nearly 100 page site with no issues. This shaved a little over 60mb out of the postmeta table on this site.
Its worth noting that this does also clear out any saved data that is hidden behind conditional logic. I consider this a feature personally, as I find the default behavior of conditional fields in ACF a bit weird and error prone. This prevents situations where a field that is hidden by logic can still have a value even if its not visible.
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.