Search code examples
amazon-web-servicesaws-api-gatewayaws-sdkaws-sdk-js

deleting a usage plan using AWS SDK JavaScript


I'm trying to delete a usage plan using AWS SDK by invoking the deleteUsagePlan function and passing the usagePlanId param as follow:

  const params: AWSNXR.APIGateway.DeleteUsagePlanRequest = {
    usagePlanId: usage_plan_id,
  };

  const apigw = new AWS.APIGateway();

  const response = await apigw.deleteUsagePlan(params).promise();

But for some reason, I'm not able to do so and I'm getting this error back: BadRequestException: Cannot delete Usage Plan USAGE_PLAN_ID_HERE because there are API Stages associated with it, including API_ID_HERE:test

Steps to reproduce:

1- create a usage plan with the params below

name: "my_usage_plan", apiStages: [ apiId: "API_ID_HERE", stage: "test" ]

2- delete the usage plan

usagePlanId: USAGE_PLAN_ID

I kept getting an error in step #2 saying: BadRequestException: Cannot delete Usage Plan USAGE_PLAN_ID_HERE because there are API Stages associated with it, including API_ID_HERE:test

Expected behavior:

the expected behavior is to be able to delete the newly created usage plan using the usage plan id

Additional content:

what I tried to fix this issue: 1- I tried deleting the test stage on the API gateway 2- I ran the delete usage plan function with the same usage plan id

the usage plan got deleted successfully without any error, however, I should be able to delete it without the need to delete a stage and re-creating it after.


Solution

  • Before deleting usage plan, you have to remove api stages, you can do this by calling updateUsagePlan as below:

    const params = {
        usagePlanId: "{usage plan id}", 
        patchOperations: [{
            op: "remove",
            path: "/apiStages",
            value: "{api gw id}:{stage name}"
        }]
     };
    const apigateway = new AWS.APIGateway();
    
    await apigateway.updateUsagePlan(params).promise();
    

    I couldn't find any documentation about it, only found solution by tracking network calls originating from aws console in browser.