private void deleteArchivedFlowNodeInstanceElements(final SAFlowNodeInstance activityInstance)
     throws SFlowNodeReadException, SBonitaSearchException, SConnectorInstanceDeletionException,
         SDataInstanceException {
   if (activityInstance instanceof SAActivityInstance) {
     dataInstanceService.deleteLocalArchivedDataInstances(
         activityInstance.getSourceObjectId(), DataInstanceContainer.ACTIVITY_INSTANCE.toString());
     connectorInstanceService.deleteArchivedConnectorInstances(
         activityInstance.getSourceObjectId(), SConnectorInstance.FLOWNODE_TYPE);
     if (SFlowNodeType.USER_TASK.equals(activityInstance.getType())
         || SFlowNodeType.MANUAL_TASK.equals(activityInstance.getType())) {
       try {
         activityService.deleteArchivedPendingMappings(activityInstance.getSourceObjectId());
       } catch (final SActivityModificationException e) {
         throw new SFlowNodeReadException(e);
       }
     }
   }
 }
 private void deleteArchivedFlowNodeInstances(final long processInstanceId)
     throws SFlowNodeReadException, SBonitaSearchException, SConnectorInstanceDeletionException,
         SFlowNodeDeletionException, SDataInstanceException {
   List<SAFlowNodeInstance> activityInstances;
   do {
     activityInstances =
         activityService.getArchivedFlowNodeInstances(processInstanceId, 0, BATCH_SIZE);
     final HashSet<Long> orgActivityIds = new HashSet<Long>();
     final ArrayList<SAFlowNodeInstance> orgActivities = new ArrayList<SAFlowNodeInstance>();
     for (final SAFlowNodeInstance activityInstance : activityInstances) {
       if (!orgActivityIds.contains(activityInstance.getSourceObjectId())) {
         orgActivityIds.add(activityInstance.getSourceObjectId());
         orgActivities.add(activityInstance);
       }
       activityService.deleteArchivedFlowNodeInstance(activityInstance);
     }
     for (final SAFlowNodeInstance orgActivity : orgActivities) {
       deleteArchivedFlowNodeInstanceElements(orgActivity);
     }
   } while (!activityInstances.isEmpty());
 }