Error deleting API Gateway Deployment (XXXX): BadRequestException:

Here is the case,
I am managing the api deployment and stage separately and when I need to add or change in api method or resource I have to delete the stage first or it gives the mentioned error even I am using the triggers in deployment but it doesn’t work and I also have an issue with this that whenever deployment runs it overwrites the old deployment to new and I don’t get any older version of it. Here is my code

resource “aws_api_gateway_stage” “stage” {
stage_name = var.environment
rest_api_id = aws_api_gateway_rest_api.api.id
deployment_id = aws_api_gateway_deployment.deployment.id
access_log_settings {
destination_arn = aws_cloudwatch_log_group.api.arn
format = replace(replace(jsondecode(jsonencode(file(“format.json”))), “\n”, " "), " ", “”)
}
}
error deleting API Gateway Deployment (XXXX): BadRequestException: Active stages pointing to this deployment must be moved or deleted

resource “aws_api_gateway_method_settings” “settings” {
rest_api_id = aws_api_gateway_rest_api.api.id
stage_name = aws_api_gateway_stage.stage.stage_name
method_path = “/
settings {
metrics_enabled = true
logging_level = “ERROR”
data_trace_enabled = true
}
}

resource “aws_api_gateway_deployment” “deployment” {
depends_on = [aws_api_gateway_method.documentresource, aws_api_gateway_method.slresource, aws_api_gateway_method.dtresource, aws_api_gateway_method.dtnresource, aws_api_gateway_integration.documentintegration, aws_api_gateway_integration.slintegration, aws_api_gateway_integration.dtintegration, aws_api_gateway_integration.dtnintegration, aws_api_gateway_method.getprofile, aws_api_gateway_integration.getprofile]
rest_api_id = aws_api_gateway_rest_api.api.id
/variables = {
deployed_at = “${timestamp()}”
}
/
triggers = {
redeployment = sha1(join(",", list(
jsonencode(aws_api_gateway_rest_api.api),
jsonencode(aws_api_gateway_method_response.getprofile),
jsonencode(aws_api_gateway_method.getprofile),
jsonencode(aws_api_gateway_resource.getprofile),
jsonencode(aws_api_gateway_integration.getprofile),
jsonencode(aws_api_gateway_method_response.dtn_put_method_response),
jsonencode(aws_api_gateway_method.dtnputresource),
jsonencode(aws_api_gateway_resource.dtnresource),
jsonencode(aws_api_gateway_integration.dtnputintegration),
jsonencode(aws_api_gateway_method.dtnresource),
jsonencode(aws_api_gateway_resource.dtnresource),
jsonencode(aws_api_gateway_method_response.dtn_method_response),
jsonencode(aws_api_gateway_integration.dtnintegration),
jsonencode(aws_api_gateway_method_response.dt_method_response),
jsonencode(aws_api_gateway_method.dtresource),
jsonencode(aws_api_gateway_resource.dtresource),
jsonencode(aws_api_gateway_integration.dtintegration),
jsonencode(aws_api_gateway_method_response.slrmethod_response),
jsonencode(aws_api_gateway_method.slresource),
jsonencode(aws_api_gateway_resource.slresource),
jsonencode(aws_api_gateway_integration.slintegration),
jsonencode(aws_api_gateway_method_response.ds_method_response),
jsonencode(aws_api_gateway_method.documentresource),
jsonencode(aws_api_gateway_resource.documentresource),
jsonencode(aws_api_gateway_integration.documentintegration),
jsonencode(aws_api_gateway_method.archivedoc),
jsonencode(aws_api_gateway_integration.archivedoc)
)))
}
}

resource “aws_api_gateway_domain_name” “api” {
domain_name = var.custom_domain
regional_certificate_arn = var.ssl_certificate
security_policy = “TLS_1_2”
endpoint_configuration {
types = [“REGIONAL”]
}
tags = {
zone = module.agi_naming.zone
environment = module.agi_naming.env
purpose = module.agi_naming.purpose
}
}

resource “aws_api_gateway_base_path_mapping” “api” {
depends_on = [aws_api_gateway_domain_name.api, aws_api_gateway_stage.stage]
api_id = aws_api_gateway_rest_api.api.id
stage_name = aws_api_gateway_stage.stage.stage_name
domain_name = aws_api_gateway_domain_name.api.domain_name

}