Is there a way to delete images from WordPress media library programmatically?

This can be achieved with wp_delete_attachment( $attachment->ID );

Here’s an example to delete all attachments related to a post:

// Delete Attachments from Post ID 25
$attachments = get_posts(
    array(
        'post_type'      => 'attachment',
        'posts_per_page' => -1,
        'post_status'    => 'any',
        'post_parent'    => 25,
    )
);
foreach ( $attachments as $attachment ) {
    wp_delete_attachment( $attachment->ID );
}