code: def delete_orphan_unique_identities(es, sortinghat_db, current_data_source, active_data_sources):
      def get_uuids_in_index(target_uuids):
          page = es.search(
              index=IDENTITIES_INDEX,
              scroll="360m",
              size=SIZE_SCROLL_IDENTITIES_INDEX,
              body={
                  "query": {
                      "bool": {
                          "filter": [
                              {
                                  "terms": {
                                      "sh_uuid": target_uuids
                                  }
                              }
                          ]
                      }
                  }
              }
          )
          hits = []
          if page['hits']['total'] != 0:
              hits = page['hits']['hits']
          return hits
      def delete_unique_identities(target_uuids):
          count = 0
          for uuid in target_uuids:
              success = SortingHat.remove_unique_identity(sortinghat_db, uuid)
              count = count + 1 if success else count
          return count
      def delete_identities(unique_ident, data_sources):
          count = 0
          for ident in unique_ident.identities:
              if ident.source not in data_sources:
                  success = SortingHat.remove_identity(sortinghat_db, ident.id)
                  count = count + 1 if success else count
          return count
      def has_identities_in_data_sources(unique_ident, data_sources):
          in_active = False
          for ident in unique_ident.identities:
              if ident.source in data_sources:
                  in_active = True
                  break
          return in_active
      deleted_unique_identities = 0
      deleted_identities = 0
      uuids_to_process = []
      for unique_identity in SortingHat.unique_identities(sortinghat_db):
          if not has_identities_in_data_sources(unique_identity, active_data_sources):
              deleted_unique_identities += delete_unique_identities([unique_identity.uuid])
              continue
          deleted_identities += delete_identities(unique_identity, active_data_sources)
          if not has_identities_in_data_sources(unique_identity, [current_data_source]):
              continue
          uuids_to_process.append(unique_identity.uuid)
          if len(uuids_to_process) != SIZE_SCROLL_IDENTITIES_INDEX:
              continue
          results = get_uuids_in_index(uuids_to_process)
          uuids_found = [item['_source']['sh_uuid'] for item in results]
          orphan_uuids = set(uuids_to_process) - set(uuids_found)
          deleted_unique_identities += delete_unique_identities(orphan_uuids)
          uuids_to_process = []
      if uuids_to_process:
          results = get_uuids_in_index(uuids_to_process)
          uuids_found = [item['_source']['sh_uuid'] for item in results]
          orphan_uuids = set(uuids_to_process) - set(uuids_found)
          deleted_unique_identities += delete_unique_identities(orphan_uuids)
      logger.debug("[identities retention] Total orphan unique identities deleted from SH: %i",
                   deleted_unique_identities)
      logger.debug("[identities retention] Total identities in non-active data sources deleted from SH: %i",
                   deleted_identities)