//snippet-sourcedescription:[DetachRolePolicy.java demonstrates how to detach a policy from an IAM role.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[iam]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[03/02/2020]
//snippet-sourceauthor:[scmacdon-aws]
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.example.iam;
// snippet-start:[iam.java2.detach_role_policy.complete]
// snippet-start:[iam.java2.detach_role_policy.import]
import software.amazon.awssdk.services.iam.model.DetachRolePolicyRequest;
import software.amazon.awssdk.services.iam.model.DetachRolePolicyResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.iam.IamClient;
import software.amazon.awssdk.services.iam.model.IamException;

// snippet-end:[iam.java2.detach_role_policy.import]
/**
 * Detaches a policy from a role
 */
public class DetachRolePolicy {
    public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply a role name and policy arn\n" +
                        "Ex: DetachRolePolicy <role-name> <policy-arn>>\n";

       if (args.length != 2) {
            System.out.println(USAGE);
            System.exit(1);
       }

        String roleName = args[0];
        String policyArn = args[1];

        // snippet-start:[iam.java2.detach_role_policy.main]
        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder().region(region).build();

        try {

            DetachRolePolicyRequest request = DetachRolePolicyRequest.builder()
                .roleName(roleName)
                .policyArn(policyArn).build();

             DetachRolePolicyResponse response = iam.detachRolePolicy(request);
             // snippet-end:[iam.java2.detach_role_policy.main]

            System.out.println("Successfully detached policy " + policyArn +
                " from role " + roleName);

        } catch (IamException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        System.out.println("Done");
    }
}
// snippet-end:[iam.java2.detach_role_policy.complete]

@@ -190,19 +190,23 @@
 ervice:%5B
-iam
+AWS IAM
 %5D%0D%0A//sni
@@ -930,64 +930,8 @@
 m;%0D%0A
-// snippet-start:%5Biam.java2.detach_role_policy.complete%5D
 %0D%0A//
@@ -1301,18 +1301,16 @@
 ption;%0D%0A
-%0D%0A
 // snipp
@@ -1355,16 +1355,18 @@
 mport%5D%0D%0A
+%0D%0A
 /**%0D%0A * 
@@ -1676,16 +1676,17 @@
 %0A       
+ 
 if (args
@@ -1778,16 +1778,17 @@
 %0A       
+ 
 %7D%0D%0A%0D%0A   
@@ -1863,70 +1863,8 @@
 %0D%0A%0D%0A
-        // snippet-start:%5Biam.java2.detach_role_policy.main%5D%0D%0A
     
@@ -1966,32 +1966,279 @@
 on).build();%0D%0A%0D%0A
+        detachPolicy(iam, roleName, policyArn);%0D%0A        System.out.println(%22Done%22);%0D%0A%0D%0A    %7D%0D%0A%0D%0A    // snippet-start:%5Biam.java2.detach_role_policy.main%5D%0D%0A    public static void detachPolicy(IamClient iam, String roleName, String policyArn ) %7B%0D%0A%0D%0A
         try %7B%0D%0A%0D
@@ -2490,76 +2490,12 @@
 t);%0D
-%0A             // snippet-end:%5Biam.java2.detach_role_policy.main%5D%0D
 %0A%0D%0A
+ 
     
@@ -2771,47 +2771,8 @@
     
-System.out.println(%22Done%22);%0D%0A    %7D%0D%0A%7D%0D%0A
 // s
@@ -2816,15 +2816,21 @@
 icy.
-complete%5D
+main%5D%0D%0A    %7D%0D%0A%7D
 %0D%0A

Update DetachRolePolicy.java