def remove_bot_publication()

in packages/blueprints/gen-ai-chatbot/static-assets/chatbot-genai-components/backend/python/app/usecases/publication.py [0:0]


def remove_bot_publication(user: User, bot_id: str):
    """Remove published bot by id."""
    bot = _fetch_bot_with_permission_check(user, bot_id)

    if bot.published_api_codebuild_id is None:
        raise ValueError(f"Bot {bot_id} is not published.")

    # If Codebuild is not succeeded, just delete bot attribute from DDB and return
    if bot.published_api_codebuild_id is not None:
        codebuild_status = find_build_status_by_build_id(bot.published_api_codebuild_id)
        if codebuild_status not in ["SUCCEEDED", "FAILED"]:
            raise ValueError(
                f"Bot {bot_id} publication is requested (build id: {bot.published_api_codebuild_id}) but not completed. Wait until the publication is completed."
            )

    # Before delete cfn stack, delete all api keys
    try:
        stack = find_stack_by_bot_id(bot_id)
    except RecordNotFoundError:
        delete_bot_publication(bot.owner_user_id, bot_id)
        return

    if stack.stack_status == "CREATE_COMPLETED":
        usage_plan = find_usage_plan_by_id(stack.api_usage_plan_id)  # type: ignore
        for key_id in usage_plan.key_ids:
            delete_api_key(key_id)

    # Delete `ApiPublishmentStack` by CloudFormation
    delete_stack_by_bot_id(bot_id)

    # Delete bot attribute
    delete_bot_publication(bot.owner_user_id, bot_id)
    return