Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Delete an inline IAM policy from a user using an Amazon SDK
The following code examples show how to delete an inline IAM policy from a user.
To avoid security risks, don't use IAM users for authentication when developing purpose-built software
or working with real data. Instead, use federation with an identity provider such as
Amazon IAM Identity Center.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in
context in the following code example:
- .NET
-
- Amazon SDK for .NET
-
/// <summary>
/// Delete an IAM user policy.
/// </summary>
/// <param name="policyName">The name of the IAM policy to delete.</param>
/// <param name="userName">The username of the IAM user.</param>
/// <returns>A Boolean value indicating the success of the action.</returns>
public async Task<bool> DeleteUserPolicyAsync(string policyName, string userName)
{
var response = await _IAMService.DeleteUserPolicyAsync(new DeleteUserPolicyRequest { PolicyName = policyName, UserName = userName });
return response.HttpStatusCode == System.Net.HttpStatusCode.OK;
}
- Go
-
- SDK for Go V2
-
// UserWrapper encapsulates user actions used in the examples.
// It contains an IAM service client that is used to perform user actions.
type UserWrapper struct {
IamClient *iam.Client
}
// DeleteUserPolicy deletes an inline policy from a user.
func (wrapper UserWrapper) DeleteUserPolicy(userName string, policyName string) error {
_, err := wrapper.IamClient.DeleteUserPolicy(context.TODO(), &iam.DeleteUserPolicyInput{
PolicyName: aws.String(policyName),
UserName: aws.String(userName),
})
if err != nil {
log.Printf("Couldn't delete policy from user %v. Here's why: %v\n", userName, err)
}
return err
}
- Ruby
-
- SDK for Ruby
-
# Deletes a user. If the user has inline policies or access keys, they are deleted
# before the user is deleted.
#
# @param user [Aws::IAM::User] The user to delete.
def delete_user(user)
user.policies.each do |policy|
name = policy.name
policy.delete
puts("Deleted user policy #{name}.")
end
user.access_keys.each do |key|
key.delete
puts("Deleted access key for user #{user.name}.")
end
name = user.name
user.delete
puts("Deleted user #{name}.")
rescue Aws::Errors::ServiceError => e
puts("Couldn't detach policies and delete user #{user.name}. Here's why:")
puts("\t#{e.code}: #{e.message}")
end
- Rust
-
- SDK for Rust
-
This documentation is for an SDK in preview release. The SDK is subject to change and should not be used in production.
pub async fn delete_user_policy(
client: &iamClient,
user: &User,
policy_name: &str,
) -> Result<(), SdkError<DeleteUserPolicyError>> {
client
.delete_user_policy()
.user_name(user.user_name.as_ref().unwrap())
.policy_name(policy_name)
.send()
.await?;
Ok(())
}
- Swift
-
- SDK for Swift
-
This is prerelease documentation for an SDK in preview release. It is subject to change.
func deleteUserPolicy(user: IAMClientTypes.User, policyName: String) async throws {
let input = DeleteUserPolicyInput(
policyName: policyName,
userName: user.userName
)
do {
_ = try await iamClient.deleteUserPolicy(input: input)
} catch {
throw error
}
}
For a complete list of Amazon SDK developer guides and code examples, see
Using IAM with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.