Remove tags
You can call the UntagResources operation to remove tags from Simple Log Service resources by using Simple Log Service SDK for Java.
Prerequisites
Simple Log Service is activated. For more information, see Enable Log Service.
Simple Log Service SDK for Java is initialized. For more information, see Initialize Simple Log Service SDK for Java.
Parameter description
Request parameters
|
Parameter |
Type |
Required |
Description |
Example |
|
resourceType |
String |
Yes |
The resource type. Valid values:
|
project |
|
resourceId |
List<String> |
Yes |
The resource ID. You can specify only one resource. The format of resourceId varies based on the value of resourceType:
|
ali-test-project |
|
tags |
List<String> |
No |
The tag keys of the tags to remove. If you do not specify this parameter, all tags are removed. |
[key1,key2] |
Response parameters
For information about the response parameters, see UntagResources.
Sample code
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Tag;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.request.UntagResourcesRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UntagResourcesExample {
// In this example, the AccessKey ID and AccessKey Secret are obtained from environment variables.
static String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
static String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// Specify a Simple Log Service endpoint.
static String host = "ap-southeast-1.log.aliyuncs.com";
// Initialize the Simple Log Service client.
static Client client = new Client(host, accessId, accessKey);
private static final String RESOURCE_TYPE = "project";
private static final String RESOURCE_ID = "ali-peoject-test1";
public static void main(String[] args) {
// Create a list of tags and add a tag object.
List<Tag> tags = new ArrayList<>();
tags.add(new Tag("key1", "value1"));
// Create a list of strings to store the keys of the tags to be removed.
List<String> tagKeys = new ArrayList<String>();
// Traverse the list of tags, fetch the key of each tag, and add it to the tagKeys list.
for (Tag t : tags) {
tagKeys.add(t.getKey());
}
// Create an UntagResourcesRequest object and specify the resource type, resource ID, and tags.
UntagResourcesRequest untagResourcesRequest = new UntagResourcesRequest(RESOURCE_TYPE, Collections.singletonList(RESOURCE_ID), tagKeys);
try {
// Call the untagResources method to remove tags from the resource.
client.untagResources(untagResourcesRequest);
} catch (LogException error) {
// Print the error message.
System.out.println("Error message :" + error.GetErrorMessage());
}
}
}
References
-
For more information about tag management APIs, see the following topics: