'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 1:[1]

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?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonathan Petitcolas