Solution:
Use the save_post hook in wordpress. You can find more info here
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
The code then should be changed to this:
add_action('save_post', 'undelete_thread');
function undelete_thread($post_id) {
global $wpdb;
global $post;
if ($post->post_type = 'fep_message'){
$participants = fep_get_participants( $post->post_parent );
foreach( $participants as $participant )
{
$query ="SELECT meta_id FROM wp_postmeta WHERE post_id = %s and `meta_key` = '_fep_delete_by_%s'";
$queryp = $wpdb->prepare($query, array($post->post_parent, $participant));
if (!empty($queryp)) {
delete_post_meta($queryp,'_fep_delete_by_' . $participant);
}
}
}
}