Search code examples
shopware6shopware6-api

Shopware 6: how to delete all products via admin api


How to delete all products via admin api?

To achieve the goal i try to use the Bulk Payloads | Deleting entities

The doc says:

[...] To delete entities, the payload of an operation contains the IDs. [...]

Questions:

  • to delete all products i have to read first all product.id's?
  • or is there a alternative way with a type of "wildcard"?

My current request body (using Postman) ...:

{
    "delete-product": {
        "entity": "product",
        "action": "delete",
        "payload": []
    }
}

... response with (products remains in db):

{
    "extensions": [],
    "success": true,
    "data": {
        "delete-product": {
            "extensions": [],
            "result": []
        }
    },
    "deleted": [],
    "notFound": []
}

EDIT #1
With id's provided...:

...
const obj = {
    "delete-products": {
        "entity": "product",
        "action": "delete",
        "payload": [
            {"id": "73af65014974440b95450f471b3afed8"},
            {"id": "784f25a29e034fad9a416923f964ba8a"}
        ]
    }
}
apiClient.request({
    "url": "/_action/sync",
    "method": "POST",
    obj
})
...

... the request fails in class Symfony\\Component\\Serializer\\Encoder\\JsonDecode with message:

detail: "Syntax error"

Debugging the request, payload is missing (empty content):

  • What is wrong with the configuration of the /api/_action/sync call?

Solution

    • to delete all products i have to read first all product.id's?

    Yes, that is what you'll have to do. This is necessary to maintain the extendibility of the platform. The core or other plugins may react to the deletion of products by subscribing to an entity lifecycle event. This event includes the id of the deleted entity. Hence why it is necessary to explicitly provide the ids of the entities in the first place.