Remove user id’s stored in arrays that themselves are stored in user meta of another user

The easiest way to overcome this would be to create a custom “getter” to pull the data from the user meta which will check and clean and update “on the fly” something like this:

First create a function to check if the user exists,

function does_user_exists($user_id){
    global $wpdb;
    $user = $wpdb->get_var( $wpdb->prepare( 
        "
            SELECT user_login
            FROM $wpdb->users 
            WHERE ID = %s
        ", 
        $user_id
    ) );
    if ($user == null)
        return false;
    else
        return true;
}

then create a function that gets the meta and validates it,

function get_user_collab($user_id){
    $update_flag = false;
    $collabs = get_user_meta($user_id,'collab_meta_key',true);
    //loop over ids and check if the user exists, if not then we remove it
    foreach ((array)$collabs as $key => $uid) {
        if (!does_user_exists($uid)){
            unset($collabs[$key]);
            $update_flag = true;
        }
    }
    if ($update_flag)
        update_user_meta($user_id,'collab_meta_key',$collabs);

    return $collabs;
}

and now instead of calling get_user_meta just use get_user_collab

tech