# Buggy code
def delete_resource(
    resource_ids=None,
    resource_group_name=None,
    resource_provider_namespace=None,
    parent_resource_path=None,
    resource_type=None,
    resource_name=None,
    api_version=None,
):
    """
    Deletes the given resource(s).
    This function allows deletion of ids with dependencies on one another.
    This is done with multiple passes through the given ids.
    """
    parsed_ids = _get_parsed_resource_ids(resource_ids) or [
        _create_parsed_id(
            resource_group_name,
            resource_provider_namespace,
            parent_resource_path,
            resource_type,
            resource_name,
        )
    ]
    to_be_deleted = [
        (_get_rsrc_util_from_parsed_id(id_dict, api_version), id_dict)
        for id_dict in parsed_ids
    ]

    results = []
    from msrestazure.azure_exceptions import CloudError

    while to_be_deleted:
        logger.debug("Start new loop to delete resources.")
        operations = []
        failed_to_delete = []
        for rsrc_utils, id_dict in to_be_deleted:
            try:
                operations.append(rsrc_utils.delete())
                logger.debug("deleting", resource_dict_to_id(**id_dict))
            except CloudError as e:
                # request to delete failed, add parsed id dict back to queue
                id_dict["exception"] = str(e)
                failed_to_delete.append((rsrc_utils, id_dict))
        to_be_deleted = failed_to_delete

        # stop deleting if none deletable
        if not operations:
            break

        # all operations return result before next pass
        for operation in operations:
            results.append(operation.result())

    if to_be_deleted:
        error_msg_builder = ["Some resources failed to be deleted:"]
        for _, id_dict in to_be_deleted:
            logger.debug(id_dict["exception"])
            error_msg_builder.append(resource_dict_to_id(**id_dict))
        raise CLIError(os.linesep.join(error_msg_builder))

    return _single_or_collection(results)


# Traceback (KeyError)
. . .
msrest.http_logger : Request method: 'DELETE'
. . .
msrest.http_logger : Response status: 202
. . .
msrest.http_logger : Response content:
msrest.http_logger : b''
'subscription'
Traceback (most recent call last):
File "/opt/az/lib/python3.6/site-packages/azure/cli/main.py", line 36, in main
cmd_result = APPLICATION.execute(args)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/application.py", line 212, in execute
result = expanded_arg.func(params)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 377, in __call__
return self.handler(*args, **kwargs)
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 620, in _execute_command
reraise(*sys.exc_info())
File "/opt/az/lib/python3.6/site-packages/six.py", line 693, in reraise
raise value
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/__init__.py", line 602, in _execute_command
result = op(client, **kwargs) if client else op(**kwargs)
File "/opt/az/lib/python3.6/site-packages/azure/cli/command_modules/resource/custom.py", line 567, in delete_resource
logger.debug("deleting", resource_dict_to_id(**id_dict))
File "/opt/az/lib/python3.6/site-packages/azure/cli/core/commands/arm.py", line 126, in resource_id
rid = '/subscriptions/{subscription}'.format(**kwargs)
KeyError: 'subscription'

# Fixed code
def delete_resource(
    resource_ids=None,
    resource_group_name=None,
    resource_provider_namespace=None,
    parent_resource_path=None,
    resource_type=None,
    resource_name=None,
    api_version=None,
):
    """
    Deletes the given resource(s).
    This function allows deletion of ids with dependencies on one another.
    This is done with multiple passes through the given ids.
    """
    parsed_ids = _get_parsed_resource_ids(resource_ids) or [
        _create_parsed_id(
            resource_group_name,
            resource_provider_namespace,
            parent_resource_path,
            resource_type,
            resource_name,
        )
    ]
    to_be_deleted = [
        (_get_rsrc_util_from_parsed_id(id_dict, api_version), id_dict)
        for id_dict in parsed_ids
    ]

    results = []
    from msrestazure.azure_exceptions import CloudError

    while to_be_deleted:
        logger.debug("Start new loop to delete resources.")
        operations = []
        failed_to_delete = []
        for rsrc_utils, id_dict in to_be_deleted:
            try:
                operations.append(rsrc_utils.delete())
                resource = (
                    resource_dict_to_id(**id_dict)
                    if id_dict.get("subscription")
                    else resource_name
                )
                logger.debug("deleting", resource)
            except CloudError as e:
                # request to delete failed, add parsed id dict back to queue
                id_dict["exception"] = str(e)
                failed_to_delete.append((rsrc_utils, id_dict))
        to_be_deleted = failed_to_delete

        # stop deleting if none deletable
        if not operations:
            break

        # all operations return result before next pass
        for operation in operations:
            results.append(operation.result())

    if to_be_deleted:
        error_msg_builder = ["Some resources failed to be deleted:"]
        for _, id_dict in to_be_deleted:
            logger.debug(id_dict["exception"])
            error_msg_builder.append(resource_dict_to_id(**id_dict))
        raise CLIError(os.linesep.join(error_msg_builder))

    return _single_or_collection(results)