def delete_inactive_unique_identities(es, sortinghat_db, before_date):
    page = es.search(
        index=IDENTITIES_INDEX,
        scroll="360m",
        size=SIZE_SCROLL_IDENTITIES_INDEX,
        body={
            "query": {
                "range": {
                    "last_seen": {
                        "lte": before_date
                    }
                }
            }
        }
    )
    sid = page['_scroll_id']
    scroll_size = page['hits']['total']
    if scroll_size == 0:
        logging.warning("[identities retention] No inactive identities found in %s after %s!",
                        IDENTITIES_INDEX, before_date)
        return
    count = 0
    while scroll_size > 0:
        for item in page['hits']['hits']:
            to_delete = item['_source']['sh_uuid']
            success = SortingHat.remove_unique_identity(sortinghat_db, to_delete)
            count = count + 1 if success else count
        page = es.scroll(scroll_id=sid, scroll='60m')
        sid = page['_scroll_id']
        scroll_size = len(page['hits']['hits'])
    logger.debug("[identities retention] Total inactive identities deleted from SH: %i", count)