I have a problem that I see many redundant log groups in all my AWS accounts for my CDK (Cloud Development Kit) application. Would it be a problem if I delete these log groups through console? New to CDK and just wanted to ensure that it is safe doing so.
-
1If the log groups aren't used by any services, it shouldn't be an issue. If they're used and you delete them the consumers would fail, probably the stacks will re-create them in the next deployment you execute. – mohRamadan Sep 22 '21 at 12:04
2 Answers
Log groups, if they are not found, are either recreated when needed or ignored by most services in AWS. You are free to clean them up, but any Insight Queries, Dashboards, or potentially any custom services parsing them will be affected, but if they are yours then you can.
In your cdk you will want to set the removal_policy
property on your log groups and set it to DELETE
in python:
from aws_cdk import core as cdk
from aws_cdk import aws_logs as logs
...
log_group = logs.LogGroup(
self, "MyLogGroup",
removal_policy = cdk.RemovalPolicy.DESTROY
)
this will cause them to be deleted when your stacks are deleted or they are removed.
you can also set a retention
policy similar, that can have the logs clean themselves up after x amount of time, with the retentions
keyword in python, and logs.RetentionDays.THREE_MONTHS
or whatever you desire.

- 2,003
- 1
- 8
- 16
Just to add, I could not control most of the log groups through CDK as the log groups were autocreated, probably when a few AWS resources like ECS,lambda were created, and when they were deleted, CloudFormation generally has a retain logs policy and they got left behind. As long as the log groups aren't referenced in any cloud formation stack, you can clean up the log groups from the console.

- 101
- 13
-
1In the future, you can set the log group manually with CDK instead of letting it auto create them. Doing so gives you the ability to clean them up. You can also reference the auto created log group, usually, depending on the construct - in python it be `your_function.logGroup` but you can also just use the logRetention property to set a retention there i think. – lynkfox Sep 29 '21 at 04:45