# Delete widget programmatically

**URL:** <https://community.cumulocity.com/t/delete-widget-programmatically/5525>\
**Category:** Forum\
**Tags:** Dashboard, widget, cockpit, cumulocity\
**Created:** [December 20, 2023, 9:09am UTC](https://community.cumulocity.com/t/delete-widget-programmatically/5525 "2023-12-20T09:09:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacsystemsw](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dacsystemsw](https://community.cumulocity.com/u/dacsystemsw)\
**Post date:** [December 20, 2023, 9:09am UTC](https://community.cumulocity.com/t/delete-widget-programmatically/5525/1 "2023-12-20T09:09:58Z")

</div>

Hi,  
it’s Luca from DAC System.  
This question is related to this [former topic](https://community.cumulocity.com/t/e2e-testing-on-cumulocityiot/1470).

For automatic testing porpouses, we need to modify a dashboard programmatically. In particular, at the moment we need to delete a map widget.

The tests are perfomed on the home section of a custom Cockpit application. So, the application looks like this

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/1/6/16eee4a721724768e2f2ede1d89f134eb548f0e2.png)

On the inventory collection the home looks like this (im using Postman to simulate what I would code):

```auto
  {
            "additionParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/additionParents"
            },
            "owner": "kai.reinhardt@softwareag.com",
            "childDevices": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childDevices"
            },
            "childAssets": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childAssets"
            },
            "creationTime": "2022-02-22T08:10:20.786Z",
            "lastUpdated": "2023-12-18T09:10:57.719Z",
            "childAdditions": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childAdditions"
            },
            "assetParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/assetParents"
            },
            "deviceParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/deviceParents"
            },
            "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200",
            "id": "1200",
            "c8y_Dashboard!name!home-cockpit1": {},
            "c8y_Global": {},
            "c8y_Dashboard": {
                "children": {
                    "799324260951046": {
                        "componentId": "cluster-map-widget",
                        "classes": {
                            "card-dashboard": true,
                            "panel-title-regular": true,
                            "cluster-map-widget": true,
                            "card": true
                        },
                        "_x": 0,
                        "_y": 0,
                        "id": "799324260951046",
                        "title": "Cluster Map",
                        "_width": 7,
                        "config": {},
                        "_height": 7
                    }
                },
                "classes": {
                    "dashboard-theme-transparent": true
                },
                "c8y_IsNavigatorNode": null,
                "widgetClasses": {
                    "panel-title-regular": true
                },
                "isFrozen": false
            }
 }

```

My initial approach is to get the id of the cluster-map-widget and just delete it. To know if I’m getting things right I try to retrieve the widget with the following query

```auto
/inventory/managedObjects/7967897646768485

```

Result:

```auto
{
    "error": "inventory/Not Found",
    "message": "Finding device data from database failed : No managedObject for id '7967897646768485'!",
    "info": "https://www.cumulocity.com/guides/reference/rest-implementation//#a-name-error-reporting-a-error-reporting"
}

```

So widgets instances are not MO (?). If so I cannot use the inventory API to delete a widget even though I know its Id.

The only solution I see to this problem is to get the home MO and update it. Any better option? Am I missing something again?

**BONUS** : to retrieve the Home MO I’m performing this query:

```auto
/inventory/managedObjects?fragmentType=c8y_Dashboard!name!home-cockpit1

```

Any better solution? Is this ok?

Best  
Luca

---

<div class="post-metadata">

**Author:** ![Tristan\_Bastian](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/tristan_bastian/32/18259_2.png) [@Tristan\_Bastian](https://community.cumulocity.com/u/Tristan_Bastian)\
**Post date:** [December 20, 2023, 10:05am UTC](https://community.cumulocity.com/t/delete-widget-programmatically/5525/2 "2023-12-20T10:05:59Z")

</div>

Hi Luca,

I do not really get where you’ve taken the `7967897646768485` id from…

The widget instances are no managedObjects. They are stored in the `c8y_Dashboard.children` path of the dashboard managedObject.  
In case you want to remove/edit a widget instance of a dashboard, you would have to adjust the dashboard managedObject.

Instead of updating the managedObject, you could also delete the dashboard and create a new one with the state you actually want to verify.  
If you do not create a new dashboard managedObject, the UI will create a new one (containing the default widgets) once you visit the Home dashboard the next time.

The query that is performed in the UI is: `/inventory/managedObjects?fragmentType=c8y_Dashboard!name!home-cockpit1&pageSize=1`. You could use the same or stick with yours, the `pageSize` shouldn’t make a lot of difference.

Regards,  
Tristan

---

<div class="post-metadata">

**Author:** ![dacsystemsw](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dacsystemsw](https://community.cumulocity.com/u/dacsystemsw)\
**Post date:** [December 20, 2023, 10:18am UTC](https://community.cumulocity.com/t/delete-widget-programmatically/5525/3 "2023-12-20T10:18:01Z")

</div>

> I do not really get where you’ve taken the 7967897646768485 id from…

My bad, copy paste error. In any case, that would be the widget _id_ in the home MO

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/1/4/145b5e2416458141181569f602c9361b9ffef818.png)

Ok that’s fine. Atm I’m performing an update as I said and it works. Eventually this could be the wrong approach when dashboard structure will become more complex. I’ll try to delete and create as you suggest…

Thank you!  
Best  
Luca

---

<div class="post-metadata">

**Author:** ![dacsystemsw](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dacsystemsw](https://community.cumulocity.com/u/dacsystemsw)\
**Post date:** [December 20, 2023, 10:53am UTC](https://community.cumulocity.com/t/delete-widget-programmatically/5525/4 "2023-12-20T10:53:36Z")

</div>

Again, about the query to get the Home Managed Object:

```auto
/inventory/managedObjects?fragmentType=c8y_Dashboard!name!home-cockpit1

```

The result is:

```auto
{
    "next": "https://t1358926305.cumulocity.com/inventory/managedObjects?withTotalElements=true&pageSize=50&fragmentType=c8y_Dashboard!name!home-cockpit1&currentPage=2",
    "self": "https://t1358926305.cumulocity.com/inventory/managedObjects?withTotalElements=true&pageSize=50&fragmentType=c8y_Dashboard!name!home-cockpit1&currentPage=1",
    "managedObjects": [
        {
            "additionParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/additionParents"
            },
            "owner": "kai.reinhardt@softwareag.com",
            "childDevices": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childDevices"
            },
            "childAssets": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childAssets"
            },
            "creationTime": "2022-02-22T08:10:20.786Z",
            "lastUpdated": "2023-12-20T10:04:54.974Z",
            "childAdditions": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/childAdditions"
            },
            "deviceParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/deviceParents"
            },
            "assetParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200/assetParents"
            },
            "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1200",
            "id": "1200",
            "c8y_Dashboard!name!home-cockpit1": {},
            "c8y_Global": {},
            "c8y_Dashboard": {
                "children": {},
                "classes": {
                    "dashboard-theme-transparent": true
                },
                "c8y_IsNavigatorNode": null,
                "widgetClasses": {
                    "panel-title-regular": true
                },
                "isFrozen": false
            }
        },
        {
            "additionParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/additionParents"
            },
            "owner": "kai.reinhardt@softwareag.com",
            "childDevices": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/childDevices"
            },
            "childAssets": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/childAssets"
            },
            "creationTime": "2022-03-11T09:23:06.703Z",
            "lastUpdated": "2023-12-20T10:04:54.979Z",
            "childAdditions": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/childAdditions"
            },
            "deviceParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/deviceParents"
            },
            "assetParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029/assetParents"
            },
            "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/1016029",
            "id": "1016029",
            "c8y_Dashboard!name!home-cockpit1": {},
            "c8y_Global": {},
            "c8y_Dashboard": {
                "children": {},
                "widgetClasses": {
                    "dashboard-theme-light": true,
                    "panel-title-regular": true
                }
            }
        },
        {
            "additionParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/additionParents"
            },
            "owner": "luca.brena@dacsystem.ch",
            "childDevices": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/childDevices"
            },
            "childAssets": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/childAssets"
            },
            "creationTime": "2022-03-29T10:30:07.839Z",
            "lastUpdated": "2023-12-20T10:04:54.980Z",
            "childAdditions": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/childAdditions"
            },
            "deviceParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/deviceParents"
            },
            "assetParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046/assetParents"
            },
            "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/3565046",
            "id": "3565046",
            "c8y_Dashboard!name!home-cockpit1": {},
            "c8y_Global": {},
            "c8y_Dashboard": {
                "children": {},
                "widgetClasses": {
                    "dashboard-theme-light": true,
                    "panel-title-regular": true
                }
            }
        },
        {
            "additionParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/additionParents"
            },
            "owner": "maurizio.santovito@dacsystem.ch",
            "childDevices": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/childDevices"
            },
            "childAssets": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/childAssets"
            },
            "creationTime": "2022-08-09T17:02:14.532Z",
            "lastUpdated": "2023-12-20T10:04:54.977Z",
            "childAdditions": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/childAdditions"
            },
            "deviceParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/deviceParents"
            },
            "assetParents": {
                "references": [],
                "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816/assetParents"
            },
            "self": "https://t1358926305.cumulocity.com/inventory/managedObjects/34106816",
            "id": "34106816",
            "c8y_Dashboard!name!home-cockpit1": {},
            "c8y_Global": {},
            "c8y_Dashboard": {
                "children": {},
                "widgetClasses": {
                    "dashboard-theme-light": true,
                    "panel-title-regular": true
                }
            }
        }
    ],
    "statistics": {
        "pageSize": 50,
        "currentPage": 1,
        "totalElements": 4
    }
}

```

As you can see we have 4 different homes. How can I target the home from a specific application?

Taking a look at what the browser calls

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/7/0/70747563385cd61bbd5a93895748d8f28b9664ea.png)

and I spot some interesting headers, which seem to refer to the application under test called Dashboard

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/1/7/1721e9234242a33f8f853df781034546ddf30e16.png)

No luck trying to reproduce this call

Any suggestion?

Best

---

<div class="post-metadata">

**Author:** ![Tristan\_Bastian](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/tristan_bastian/32/18259_2.png) [@Tristan\_Bastian](https://community.cumulocity.com/u/Tristan_Bastian)\
**Post date:** [December 20, 2023, 11:36am UTC](https://community.cumulocity.com/t/delete-widget-programmatically/5525/5 "2023-12-20T11:36:16Z")

</div>

The `Referer` and `X-Cumulocity-Application-Key` do not influence the response you are receiving here.

I can’t really tell why you are having multiple dashboards with the `c8y_Dashboard!name!home-cockpit1` fragment, there should actually just be a single one.  
This might be related to the limited permissions you are giving to the other user accounts, but I’m not sure.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [June 17, 2024, 11:36am UTC](https://community.cumulocity.com/t/delete-widget-programmatically/5525/6 "2024-06-17T11:36:59Z")

</div>

Closed on June 17, 2024, 11:36am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
