<commit_before>import logging

from django.core.management import BaseCommand, CommandError

from corehq.apps.domain.models import Domain
from corehq.apps.es import AppES, CaseES, CaseSearchES, FormES, GroupES, UserES
from corehq.apps.es.registry import registry_entry
from corehq.apps.es.transient_util import doc_adapter_from_info

logger = logging.getLogger(__name__)


class Command(BaseCommand):
    """
    Intended for use in the event that a domain has been deleted, but ES docs have not been fully cleaned up
    """

    def add_arguments(self, parser):
        parser.add_argument('domain')

    def handle(self, domain, **options):
        domain_obj = Domain.get_by_name(domain)
        if domain_obj and not domain_obj.doc_type.endswith('-Deleted'):
            raise CommandError(
                f"{domain} has not been deleted. This command is intended for use on deleted domains only."
            )

        for hqESQuery in [AppES, CaseES, CaseSearchES, FormES, GroupES, UserES]:
            doc_ids = hqESQuery().domain(domain).source(['_id']).run().hits
            doc_ids = [doc['_id'] for doc in doc_ids]
            if not doc_ids:
                continue
            adapter = doc_adapter_from_info(registry_entry(hqESQuery.index))
            adapter.bulk_delete(doc_ids)
            logger.info(f"Deleted {len(doc_ids)} {hqESQuery.index} for {domain} in ES")<commit_msg>use print instead of logging; modify message<commit_after>from django.core.management import BaseCommand, CommandError

from corehq.apps.domain.models import Domain
from corehq.apps.es import AppES, CaseES, CaseSearchES, FormES, GroupES, UserES
from corehq.apps.es.registry import registry_entry
from corehq.apps.es.transient_util import doc_adapter_from_info


class Command(BaseCommand):
    """
    Intended for use in the event that a domain has been deleted, but ES docs have not been fully cleaned up
    """

    def add_arguments(self, parser):
        parser.add_argument('domain')

    def handle(self, domain, **options):
        domain_obj = Domain.get_by_name(domain)
        if domain_obj and not domain_obj.doc_type.endswith('-Deleted'):
            raise CommandError(
                f"{domain} has not been deleted. This command is intended for use on deleted domains only."
            )

        for hqESQuery in [AppES, CaseES, CaseSearchES, FormES, GroupES, UserES]:
            doc_ids = hqESQuery().domain(domain).source(['_id']).run().hits
            doc_ids = [doc['_id'] for doc in doc_ids]
            if not doc_ids:
                continue
            adapter = doc_adapter_from_info(registry_entry(hqESQuery.index))
            adapter.bulk_delete(doc_ids)
            print(f"Deleted {len(doc_ids)} documents in the {hqESQuery.index} index for {domain}")
{'fraction_non_alphanumeric': 0.09038737446197992, 'fraction_numerical': 0.0, 'mean_word_length': 3.101470588235294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '938606', 'n_tokens_mistral': 824, 'n_tokens_neox': 808, 'n_words': 235}