set_post_thumbnail or media_sideload_image adds image multiple times in media library

<?php 

$post_id = get_queried_object_id();

$bla = "myImageURL";

// Set the image URL
$image_url = $bla;

// Check if the image already exists in the media library
$existing_image = get_posts(array(
    'post_type' => 'attachment',
    'meta_key' => '_wp_attached_file',
    'meta_value' => sanitize_file_name(basename($image_url))
));

if (!$existing_image) {
    // Image does not exist in the media library, so download it
    $image_id = media_sideload_image($image_url, $post_id, '', 'id');

    // Check for errors
    if (is_wp_error($image_id)) {
        print_r($image_url);
    } else {
        // The image was downloaded successfully. Now set it as the featured image for the post
        set_post_thumbnail($post_id, $image_id);
    }
} else {
    // Image already exists in the media library, so set it as the featured image for the post
    set_post_thumbnail($post_id, $existing_image[0]->ID);
}

?>