Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shivam-nagar/aa79b02b74f616f8714d51e419bd10de to your computer and use it in GitHub Desktop.
Save shivam-nagar/aa79b02b74f616f8714d51e419bd10de to your computer and use it in GitHub Desktop.
AWS ECS Deregister Task definitions Bulk
const skipTaskDefList = [ /* List of task names to skip deletion. */ ]
const AWS_REGION = "us-west-2"
const { ECSClient, ListTaskDefinitionsCommand, DeregisterTaskDefinitionCommand } = require("@aws-sdk/client-ecs");
async function deleteTaskDef(taskDefARN) {
const awsClient = new ECSClient({ region: AWS_REGION });
const deleteCommand = new DeregisterTaskDefinitionCommand({taskDefinition: taskDefARN});
await awsClient.send(deleteCommand)
.then((deregisterTaskDefinitionResponse) => {
const taskDefObj = deregisterTaskDefinitionResponse.taskDefinition;
console.log(taskDefObj.taskDefinitionArn, taskDefObj.status);
});
}
async function execute(deleteTasks=false) {
const awsClient = new ECSClient({ region: AWS_REGION });
const command = new ListTaskDefinitionsCommand({ status: 'ACTIVE', maxResults: 100 });
await awsClient.send(command)
.then((response) => {
const allTaskDefList = response['taskDefinitionArns'].filter((taskDef) => {
for (let i=0; i<skipTaskDefList.length; i++){
if(taskDef.includes(`/${skipTaskDefList[i]}:`))
return false;
}
return true;
});
console.log("Will delete following task-definition ARNs: \n", allTaskDefList);
if(!deleteTasks) {
console.log("!!! Method called in safe mode, Not deleting tasks.");
return;
}
allTaskDefList.forEach(deleteTaskDef);
});
};
// Will list ARNs, pass true to perform delete.
execute();
@shivam-nagar
Copy link
Author

shivam-nagar commented Jul 23, 2021

  1. Install @aws-sdk/client-ecs:
npm install @aws-sdk/client-ecs
yarn add @aws-sdk/client-ecs
pnpm add @aws-sdk/client-ecs
  1. Run:
node aws-ecs-deregister-task-definitions.js

Ref:
ListTaskDefinitionsCommand
DeregisterTaskDefinitionCommand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment