rammi22
rammi22

Reputation: 403

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:

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):

Upvotes: 1

Views: 1420

Answers (2)

augsteyer
augsteyer

Reputation: 1099

Indeed, what it means is that you will need a low impacting query to get all product id's, store it into a variable & delete them. Use includes:["id"] filter to just get the ID's.

Here is an example of me deleting some products in Postman. Request body:

{
    "delete-product": {
        "entity": "product",
        "action": "delete",
        "payload": {{gen_dynamic_products}}
    }
}

Pre-request script (you'll need to adjust this sightly to get your ID's):

const map = new Array(30).fill(0).map((val, index) => {
    return { id: pm.environment.get('gen_product_list_sub_' + index) };
});
pm.variables.set('gen_dynamic_products', JSON.stringify(map));

Upvotes: 1

dneustadt
dneustadt

Reputation: 13161

  • 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.

Upvotes: 0

Related Questions