Strapi – Delete an item from nested repeatable component

Strapi – Delete an item from nested repeatable component


0

I would like to know if it is possible to delete an item from a nested object via API or graphQL. I have a collection type (restaurant) with a repeatable component inside called "franchise" (Florida and New York). I would like to delete just one of those (Florida for example). Is that possible?

API GET

{
    "data": [{
        "id": 2,
        "attributes": {
            "name": "Italian Food",
            "address": null,
            "createdAt": "2023-09-16T19:13:50.831Z",
            "updatedAt": "2023-09-16T19:13:51.632Z",
            "publishedAt": "2023-09-16T19:13:51.629Z",
            "franchise": [{
                "id": 4,
                "state": "Florida"
            }, {
                "id": 5,
                "state": "New York"
            }]
        }
    }],
    "meta": {
        "pagination": {
            "page": 1,
            "pageSize": 25,
            "pageCount": 1,
            "total": 1
        }
    }
}

Strapi - Delete an item from nested repeatable component

1 Answer
1


0

The way Strapi works. There is no direct way to delete components. It is not impossible but…

In your case I would consider and refactor franchises to be a collection type rather than a repeatable component.

Here is a great talk around content modeling in Strapi https://www.youtube.com/watch?v=PDnWCnA6qTg

But if you wanted to delete the component, you would have to do a update mutation on restaurant collection type.

And pass in the whole object, minus the franchise you want to remove.

"data": [{
        "id": 2,
        "attributes": {
            "name": "Italian Food",
            "address": null,
            "createdAt": "2023-09-16T19:13:50.831Z",
            "updatedAt": "2023-09-16T19:13:51.632Z",
            "publishedAt": "2023-09-16T19:13:51.629Z",
            "franchise": [{
                "id": 5,
                "state": "New York"
            }]
        }
    }],

This will update restaurant collection type and remove Florida franchise.

Since we are not passing it in the update mutation, Strapi will delete it.

But the best approach would be to convert it to a collection type vs a component.

Hope this help.



Leave a Reply

Your email address will not be published. Required fields are marked *