Use DeleteSubscriptionFilter with an AWS SDK or CLI - Amazon CloudWatch Logs

Use DeleteSubscriptionFilter with an AWS SDK or CLI

The following code examples show how to use DeleteSubscriptionFilter.

C++
SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Include the required files.

#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/logs/CloudWatchLogsClient.h> #include <aws/logs/model/DeleteSubscriptionFilterRequest.h> #include <iostream>

Delete the subscription filter.

Aws::CloudWatchLogs::CloudWatchLogsClient cwl; Aws::CloudWatchLogs::Model::DeleteSubscriptionFilterRequest request; request.SetFilterName(filter_name); request.SetLogGroupName(log_group); auto outcome = cwl.DeleteSubscriptionFilter(request); if (!outcome.IsSuccess()) { std::cout << "Failed to delete CloudWatch log subscription filter " << filter_name << ": " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully deleted CloudWatch logs subscription " << "filter " << filter_name << std::endl; }
Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; import software.amazon.awssdk.services.cloudwatchlogs.model.DeleteSubscriptionFilterRequest; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteSubscriptionFilter { public static void main(String[] args) { final String usage = """ Usage: <filter> <logGroup> Where: filter - The name of the subscription filter (for example, MyFilter). logGroup - The name of the log group. (for example, testgroup). """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String filter = args[0]; String logGroup = args[1]; CloudWatchLogsClient logs = CloudWatchLogsClient.builder() .build(); deleteSubFilter(logs, filter, logGroup); logs.close(); } public static void deleteSubFilter(CloudWatchLogsClient logs, String filter, String logGroup) { try { DeleteSubscriptionFilterRequest request = DeleteSubscriptionFilterRequest.builder() .filterName(filter) .logGroupName(logGroup) .build(); logs.deleteSubscriptionFilter(request); System.out.printf("Successfully deleted CloudWatch logs subscription filter %s", filter); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
JavaScript
SDK for JavaScript (v3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import { DeleteSubscriptionFilterCommand } from "@aws-sdk/client-cloudwatch-logs"; import { client } from "../libs/client.js"; const run = async () => { const command = new DeleteSubscriptionFilterCommand({ // The name of the filter. filterName: process.env.CLOUDWATCH_LOGS_FILTER_NAME, // The name of the log group. logGroupName: process.env.CLOUDWATCH_LOGS_LOG_GROUP, }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
SDK for JavaScript (v2)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create the CloudWatchLogs service object var cwl = new AWS.CloudWatchLogs({ apiVersion: "2014-03-28" }); var params = { filterName: "FILTER", logGroupName: "LOG_GROUP", }; cwl.deleteSubscriptionFilter(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

suspend fun deleteSubFilter( filter: String?, logGroup: String?, ) { val request = DeleteSubscriptionFilterRequest { filterName = filter logGroupName = logGroup } CloudWatchLogsClient { region = "us-west-2" }.use { logs -> logs.deleteSubscriptionFilter(request) println("Successfully deleted CloudWatch logs subscription filter named $filter") } }

For a complete list of AWS SDK developer guides and code examples, see Using CloudWatch Logs with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.