Anchor to section titled 'undefined'

productDeleteMedia
mutation

Requires write_products access scope. Also: The user must have a permission to delete media from a product.

Deletes media for a product.


Anchor to mediaIds
mediaIds
required

The media IDs to be deleted.

Anchor to productId
productId
required

Specifies the product ID from which the media will be deleted.


Was this section helpful?

List of media IDs which were deleted.

List of product image IDs which were deleted.

The list of errors that occurred from executing the mutation.

The product associated with the deleted media.

The list of errors that occurred from executing the mutation. Use mediaUserErrors instead.


Was this section helpful?

Examples

Hide code
DescriptionCopy
mutation productDeleteMedia($mediaIds: [ID!]!, $productId: ID!) {
  productDeleteMedia(mediaIds: $mediaIds, productId: $productId) {
    deletedMediaIds
    deletedProductImageIds
    mediaUserErrors {
      field
      message
    }
    product {
      id
      title
      media(first: 5) {
        nodes {
          alt
          mediaContentType
          status
        }
      }
    }
  }
}
curl -X POST \
https://your-development-store.myshopify.com/admin/api/2024-01/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \
-d '{
"query": "mutation productDeleteMedia($mediaIds: [ID!]!, $productId: ID!) { productDeleteMedia(mediaIds: $mediaIds, productId: $productId) { deletedMediaIds deletedProductImageIds mediaUserErrors { field message } product { id title media(first: 5) { nodes { alt mediaContentType status } } } } }",
 "variables": {
    "mediaIds": [
      "gid://shopify/Video/-1",
      "gid://shopify/Video/723685877"
    ],
    "productId": "gid://shopify/Product/108828309"
  }
}'
const { admin } = await authenticate.admin(request);

const response = await admin.graphql(
  `#graphql
  mutation productDeleteMedia($mediaIds: [ID!]!, $productId: ID!) {
    productDeleteMedia(mediaIds: $mediaIds, productId: $productId) {
      deletedMediaIds
      deletedProductImageIds
      mediaUserErrors {
        field
        message
      }
      product {
        id
        title
        media(first: 5) {
          nodes {
            alt
            mediaContentType
            status
          }
        }
      }
    }
  }`,
  {
    variables: {
      "mediaIds": [
        "gid://shopify/Video/-1",
        "gid://shopify/Video/723685877"
      ],
      "productId": "gid://shopify/Product/108828309"
    },
  },
);

const data = await response.json();
session = ShopifyAPI::Auth::Session.new(
  shop: "your-development-store.myshopify.com",
  access_token: access_token
)
client = ShopifyAPI::Clients::Graphql::Admin.new(
  session: session
)

query = <<~QUERY
  mutation productDeleteMedia($mediaIds: [ID!]!, $productId: ID!) {
    productDeleteMedia(mediaIds: $mediaIds, productId: $productId) {
      deletedMediaIds
      deletedProductImageIds
      mediaUserErrors {
        field
        message
      }
      product {
        id
        title
        media(first: 5) {
          nodes {
            alt
            mediaContentType
            status
          }
        }
      }
    }
  }
QUERY

variables = {
  "mediaIds": ["gid://shopify/Video/-1", "gid://shopify/Video/723685877"],
  "productId": "gid://shopify/Product/108828309"
}

response = client.query(query: query, variables: variables)
const client = new shopify.clients.Graphql({session});
const data = await client.query({
  data: {
    "query": `mutation productDeleteMedia($mediaIds: [ID!]!, $productId: ID!) {
      productDeleteMedia(mediaIds: $mediaIds, productId: $productId) {
        deletedMediaIds
        deletedProductImageIds
        mediaUserErrors {
          field
          message
        }
        product {
          id
          title
          media(first: 5) {
            nodes {
              alt
              mediaContentType
              status
            }
          }
        }
      }
    }`,
    "variables": {
      "mediaIds": [
        "gid://shopify/Video/-1",
        "gid://shopify/Video/723685877"
      ],
      "productId": "gid://shopify/Product/108828309"
    },
  },
});
use Shopify\Clients\Graphql;

$client = new Graphql("your-development-store.myshopify.com", $accessToken);
$query = <<<QUERY
  mutation productDeleteMedia($mediaIds: [ID!]!, $productId: ID!) {
    productDeleteMedia(mediaIds: $mediaIds, productId: $productId) {
      deletedMediaIds
      deletedProductImageIds
      mediaUserErrors {
        field
        message
      }
      product {
        id
        title
        media(first: 5) {
          nodes {
            alt
            mediaContentType
            status
          }
        }
      }
    }
  }
QUERY;

$variables = [
  "mediaIds" => ["gid://shopify/Video/-1", "gid://shopify/Video/723685877"],
  "productId" => "gid://shopify/Product/108828309",
];

$response = $client->query(["query" => $query, "variables" => $variables]);
Hide code
Input variables
Copy
{
  "mediaIds": [
    "gid://shopify/Video/-1",
    "gid://shopify/Video/723685877"
  ],
  "productId": "gid://shopify/Product/108828309"
}
Hide code
Response
JSON
{
  "productDeleteMedia": {
    "deletedMediaIds": null,
    "deletedProductImageIds": null,
    "mediaUserErrors": [
      {
        "field": [
          "mediaIds"
        ],
        "message": "Media id gid://shopify/Video/-1 does not exist"
      }
    ],
    "product": {
      "id": "gid://shopify/Product/108828309",
      "title": "Draft",
      "media": {
        "nodes": [
          {
            "alt": "This is a video",
            "mediaContentType": "EXTERNAL_VIDEO",
            "status": "READY"
          },
          {
            "alt": "This is a video",
            "mediaContentType": "VIDEO",
            "status": "READY"
          },
          {
            "alt": "This is a 3d Model",
            "mediaContentType": "MODEL_3D",
            "status": "READY"
          },
          {
            "alt": "",
            "mediaContentType": "IMAGE",
            "status": "READY"
          },
          {
            "alt": "",
            "mediaContentType": "IMAGE",
            "status": "READY"
          }
        ]
      }
    }
  }
}