1

I want to delete many managed objects, selected by fragment type. There are more then 2000 elements in it. Unfortunately I can not delete all with one function call. I have to call this function many times until I have deleted all. How can I delete a list of managed objects in a sufficient way? Not defining page size did not help...

This is my current function:

    InventoryFilter filter = new InventoryFilter();
    filter.byFragmentType("xy_fragment");

    ManagedObjectCollection moc = inventoryApi.getManagedObjectsByFilter(filter);

    int count = 0;
    // max page size is 2000
    for (ManagedObjectRepresentation mo : moc.get(2000).allPages()) {
        if (mo.get("c8y_IsBinary") != null) {
            binariesApi.deleteFile(mo.getId());
        } else {
            inventoryApi.delete(mo.getId());
        }

        LOG.debug(count + " remove: " + mo.getName() + ", " + mo.getType());
        count++;
    }

    LOG.info("all objectes removed, count:" + count);
Anand
  • 6,457
  • 3
  • 12
  • 26
apes
  • 87
  • 8

3 Answers3

0

There is no bulk delete allowed on the inventory API so your method of looping through the objects is the correct approach.

A bulk delete is already a dangerous tool on the other APIs but on the inventory API it would give you the potential to accidentally delete all your data with just one call (as all data associated with a managedObject is also deleted upon the deletion of the managedObject). That is why it is not available.

TyrManuZ
  • 2,039
  • 1
  • 14
  • 23
  • Sure, i understand the potential, however, it is needed in certain use-case. How can i modify the code above to delete all elements not just max 2000? – apes May 31 '18 at 09:56
  • I am not that familiar with the SDK but on API you would just grab the next page until you are at the end. I don't know if such mechanism is directly available in the SDK. – TyrManuZ Jun 01 '18 at 07:58
  • The behavior of the sdk (in my case 8.15.0) is strange. The code above removes even more than 2000 if a large number (10000) of elements are selected. However, I never get all elements with this call `moc.get(2000).allPages() //allPages() persuade me that all pages` only if e<2000 – apes Jun 01 '18 at 12:47
0

I solved the problem by calling the method until no elements can be found any more. It is not nice but I have no other idea.

    public synchronized void removeManagedObjects(String deviceTypeKey) {
    int count = 0;
    do {
        count = deleteManagedObjectes(deviceTypeKey);
    }while(count > 0);
}

private int deleteManagedObjectes(String deviceTypeKey) {
    InventoryFilter filter = new InventoryFilter();
    filter.byFragmentType("xy_fragment");

    ManagedObjectCollection moc = inventoryApi.getManagedObjectsByFilter(filter);
    int count = 0;

    if(moc == null) {
        LOG.info("ManagedObjectCollection are NULL");
        return count;
    }

    for (ManagedObjectRepresentation mo : moc.get(2000).allPages()) {
        if (mo.get("c8y_IsBinary") != null) {
            binariesApi.deleteFile(mo.getId());
        } else {
            inventoryApi.delete(mo.getId());
        }

        LOG.debug(count + " remove: " + mo.getName() + ", " + mo.getType());
        count++;
    }

    LOG.info("all objectes removed, count:" + count);
    return count;
}
apes
  • 87
  • 8
0

By calling moc.get(2000).allPages() you already obtain an iterator that queries following pages on demand as you iterate over it.

The problem you are facing is caused by deleting elements from the same list you are iterating over. You delete element from the first page, but once the second page is queried from the server it does not contain the expected elements anymore because you already deleted the first page. Now all elements are shifted forward by your page size.

You can avoid all of that by making a local copy of all elements you want to delete first:

List<ManagedObjectRepresentation> allObjects = Lists.newArrayList( moc.get(2000).allPages())
for (ManagedObjectRepresentation mo : allObjects) {
  //delete here   
}
apes
  • 87
  • 8
l2p
  • 420
  • 3
  • 9