Search code examples
amazon-web-servicesuser-controlsamazon-iam

Cannot delete entity, must remove tokens from principal first


while trying to delete aws user from AWS Java SDK. i am getting following error:

Cannot delete entity, must remove tokens from principal first.

the relevant code snippet is:

DeleteUserRequest deleteUserRequest = DeleteUserRequest.builder().userName(userName).build();
iam.deleteUser(deleteUserRequest);

Solution

  • I faced the same issue with Terraform. The user had some access tokens and MFA devices configured on their account. They have created it manually, hence, Terraform didn't know about that. So, it was not able to delete the user due to the exact same error.

    Deleting the MFA tokens and the manually generated access tokens fixed the issue.

    Perhaps can you automate it with Java?