Cannot delete product images via Shopify REST API

Problem: Images Are Not Being Deleted From Products

I have the following custom function in Python to communicate with the Shopify REST API via requests to delete images from products:

headers = {"Accept": "application/json", "Content-Type": "application/json"}

def delete_product_image(shop_url, product_id, image_id,headers):
    del_r = requests.delete(f'{shop_url}/products/{product_id}/images/{image_id}.json()',headers=headers)
    if del_r.status_code in [200, 201]:
        print(f'image {image_id} of product {product_id} removed')
    else:
        print(f'Error: {del_r.status_code} - {del_r.text}')
        time.sleep(1)
        delete_product_image(shop_url, product_id, image_id)

When I execute the delete_product_image(shop_url, product_id, image_id,headers) function, the del_r status code returns 200, but the images are not deleted from the products.

What is causing this issue?

The issue is with the requests.delete() call. You are passing "{image_id}.json()" as the path parameter, which is incorrect. The .json() should not be included in the path.

To fix the issue, modify the delete_product_image() function as follows:

def delete_product_image(shop_url, product_id, image_id, headers):
    del_r = requests.delete(f'{shop_url}/products/{product_id}/images/{image_id}.json', headers=headers)
    if del_r.status_code in [200, 201]:
        print(f'image {image_id} of product {product_id} removed')
    else:
        print(f'Error: {del_r.status_code} - {del_r.text}')
        time.sleep(1)
        delete_product_image(shop_url, product_id, image_id, headers)

Now, when you execute the delete_product_image() function, the images should be deleted successfully.