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).
Use DeleteAnomalyDetector
with an Amazon SDK or CLI
The following code examples show how to use DeleteAnomalyDetector
.
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 a single metric anomaly detector.
/// </summary>
/// <param name="anomalyDetector">The anomaly detector to delete.</param>
/// <returns>True if successful.</returns>
public async Task<bool> DeleteAnomalyDetector(SingleMetricAnomalyDetector anomalyDetector)
{
var deleteAnomalyDetectorResponse = await _amazonCloudWatch.DeleteAnomalyDetectorAsync(
new DeleteAnomalyDetectorRequest()
{
SingleMetricAnomalyDetector = anomalyDetector
});
return deleteAnomalyDetectorResponse.HttpStatusCode == HttpStatusCode.OK;
}
- Java
-
- SDK for Java 2.x
-
public static void deleteAnomalyDetector(CloudWatchClient cw, String fileName) {
try {
// Read values from the JSON file.
JsonParser parser = new JsonFactory().createParser(new File(fileName));
com.fasterxml.jackson.databind.JsonNode rootNode = new ObjectMapper().readTree(parser);
String customMetricNamespace = rootNode.findValue("customMetricNamespace").asText();
String customMetricName = rootNode.findValue("customMetricName").asText();
SingleMetricAnomalyDetector singleMetricAnomalyDetector = SingleMetricAnomalyDetector.builder()
.metricName(customMetricName)
.namespace(customMetricNamespace)
.stat("Maximum")
.build();
DeleteAnomalyDetectorRequest request = DeleteAnomalyDetectorRequest.builder()
.singleMetricAnomalyDetector(singleMetricAnomalyDetector)
.build();
cw.deleteAnomalyDetector(request);
System.out.println("Successfully deleted the Anomaly Detector.");
} catch (CloudWatchException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
}
}
- Kotlin
-
- SDK for Kotlin
-
suspend fun deleteAnomalyDetector(fileName: String) {
// Read values from the JSON file.
val parser = JsonFactory().createParser(File(fileName))
val rootNode = ObjectMapper().readTree<JsonNode>(parser)
val customMetricNamespace = rootNode.findValue("customMetricNamespace").asText()
val customMetricName = rootNode.findValue("customMetricName").asText()
val singleMetricAnomalyDetectorVal =
SingleMetricAnomalyDetector {
metricName = customMetricName
namespace = customMetricNamespace
stat = "Maximum"
}
val request =
DeleteAnomalyDetectorRequest {
singleMetricAnomalyDetector = singleMetricAnomalyDetectorVal
}
CloudWatchClient { region = "us-east-1" }.use { cwClient ->
cwClient.deleteAnomalyDetector(request)
println("Successfully deleted the Anomaly Detector.")
}
}
For a complete list of Amazon SDK developer guides and code examples, see
Using CloudWatch with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.