def cleanup()

in dev/breeze/src/airflow_breeze/commands/main_command.py [0:0]


def cleanup(all: bool):
    if all:
        get_console().print(
            "\n[info]Removing cache of parameters, clean up docker cache "
            "and remove locally downloaded images[/]"
        )
    else:
        get_console().print("[info]Removing cache of parameters, and cleans up docker cache[/]")
    if all:
        docker_images_command_to_execute = [
            "docker",
            "images",
            "--filter",
            "label=org.apache.airflow.image",
            "--format",
            "{{.Repository}}:{{.Tag}}",
        ]
        command_result = run_command(docker_images_command_to_execute, text=True, capture_output=True)
        images = command_result.stdout.splitlines() if command_result and command_result.stdout else []
        if images:
            get_console().print("[info]Removing images:[/]")
            for image in images:
                get_console().print(f"[info] * {image}[/]")
            get_console().print()
            docker_rmi_command_to_execute = [
                "docker",
                "rmi",
                "--force",
            ]
            docker_rmi_command_to_execute.extend(images)
            given_answer = user_confirm("Are you sure with the removal?")
            if given_answer == Answer.YES:
                run_command(docker_rmi_command_to_execute, check=False)
            elif given_answer == Answer.QUIT:
                sys.exit(0)
        else:
            get_console().print("[info]No locally downloaded images to remove[/]\n")
    get_console().print("Removing networks created by breeze")
    given_answer = user_confirm("Are you sure with the removal of docker networks created by breeze?")
    if given_answer == Answer.YES:
        remove_docker_networks()
    get_console().print("Removing volumes created by breeze")
    given_answer = user_confirm("Are you sure with the removal of docker volumes created by breeze?")
    if given_answer == Answer.YES:
        remove_docker_volumes()
    get_console().print("Pruning docker images")
    given_answer = user_confirm("Are you sure with the removal of docker images?")
    if given_answer == Answer.YES:
        system_prune_command_to_execute = ["docker", "system", "prune", "-f"]
        run_command(
            system_prune_command_to_execute,
            check=False,
        )
    elif given_answer == Answer.QUIT:
        sys.exit(0)
    get_console().print(f"Removing build cache dir {BUILD_CACHE_PATH}")
    given_answer = user_confirm("Are you sure with the removal?")
    if given_answer == Answer.YES:
        if not get_dry_run():
            shutil.rmtree(BUILD_CACHE_PATH, ignore_errors=True)
    get_console().print("Uninstalling airflow and removing configuration")
    given_answer = user_confirm("Are you sure with the uninstall / remove?")
    if given_answer == Answer.YES:
        if not get_dry_run():
            shutil.rmtree(AIRFLOW_HOME_PATH, ignore_errors=True)
            AIRFLOW_HOME_PATH.mkdir(exist_ok=True, parents=True)
            run_command(["uv", "pip", "uninstall", "apache-airflow"], check=False)
    elif given_answer == Answer.QUIT:
        sys.exit(0)

    to_be_excluded_from_deletion = (
        # dirs
        ".idea/",  # Pycharm config
        ".vscode/",  # VSCode config
        ".venv/",
        "files/",
        "logs/",
        # files
        ".bash_history",
        ".bash_aliases",
    )

    get_console().print(
        "Removing build file and git untracked files. This also removes files ignored in .gitignore.\n"
        f"The following files will not be removed: `{to_be_excluded_from_deletion}`."
    )
    given_answer = user_confirm("Are you sure with the removal of build files?")
    if given_answer == Answer.YES:
        system_prune_command_to_execute = ["git", "clean", "-fdx"]
        for excluded_object in to_be_excluded_from_deletion:
            system_prune_command_to_execute.extend(["-e", excluded_object])

        run_command(
            system_prune_command_to_execute,
            check=False,
        )
    elif given_answer == Answer.QUIT:
        sys.exit(0)