Support

Account

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.