# Buggy code
def process(self, resources):
    client = local_session(self.manager.session_factory).client("iam")
    error = None
    if self.data.get("force", False):
        policy_setter = self.manager.action_registry["set-policy"](
            {"state": "detached", "arn": "*"}, self.manager
        )
        policy_setter.process(resources)
    for r in resources:
        try:
            client.delete_role(RoleName=r["RoleName"])
        except client.exceptions.DeleteConflictException as e:
            self.log.warning(
                "Role:%s cannot be deleted, set force to detach policy and delete"
                % r["Arn"]
            )
            error = e
        except client.exceptions.NoSuchEntityException:
            continue
        except client.exceptions.UnmodifiableEntityException:
            continue
    if error:
        raise error


# Traceback (botocore.errorfactory.DeleteConflictException)
2020-09-08 10:48:33,898: custodian.actions:WARNING Role:arn:aws:iam::role cannot be deleted, set force to detach policy and delete
2020-09-08 10:48:34,042: custodian.output:ERROR Error while executing policy
and delete
2020-09-08 10:48:34,042: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
File "site-packages/c7n/policy.py", line 313, in run
results = a.process(resources)
File "site-packages/c7n/resources/iam.py", line 1095, in process
raise error
File "site-packages/c7n/resources/iam.py", line 1084, in process
client.delete_role(RoleName=r['RoleName'])
File "site-packages/botocore/client.py", line 316, in _api_call
return self._make_api_call(operation_name, kwargs)
File "site-packages/botocore/client.py", line 635, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.DeleteConflictException: An error occurred (DeleteConflict) when calling the DeleteRole operation: Cannot delete entity, must delete policies first.
2020-09-08 10:48:34,044: custodian.commands:ERROR Error while executing policy remove-role, continuing
Traceback (most recent call last):
File "site-packages/c7n/commands.py", line 272, in run
policy()
File "site-packages/c7n/policy.py", line 1175, in __call__
resources = mode.run()
File "site-packages/c7n/policy.py", line 313, in run
results = a.process(resources)
File "site-packages/c7n/resources/iam.py", line 1095, in process
raise error
File "site-packages/c7n/resources/iam.py", line 1084, in process
client.delete_role(RoleName=r['RoleName'])
File "site-packages/botocore/client.py", line 316, in _api_call
return self._make_api_call(operation_name, kwargs)
File "site-packages/botocore/client.py", line 635, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.DeleteConflictException: An error occurred (DeleteConflict) when calling the DeleteRole operation: Cannot delete entity, must delete policies first.
2020-09-08 10:48:34,044: custodian.commands:ERROR The following policies had errors while executing
- remove-role

# Fixed code
def process(self, resources):
    client = local_session(self.manager.session_factory).client("iam")
    error = None
    if self.data.get("force", False):
        policy_setter = self.manager.action_registry["set-policy"](
            {"state": "detached", "arn": "*"}, self.manager
        )
        policy_setter.process(resources)

    for r in resources:
        if self.data.get("force", False):
            self.detach_inline_policies(client, r)
        try:
            client.delete_role(RoleName=r["RoleName"])
        except client.exceptions.DeleteConflictException as e:
            self.log.warning(
                "Role:%s cannot be deleted, set force to detach policy and delete"
                % r["Arn"]
            )
            error = e
        except (
            client.exceptions.NoSuchEntityException,
            client.exceptions.UnmodifiableEntityException,
        ):
            continue
    if error:
        raise error