Created
July 23, 2021 17:50
-
-
Save shivam-nagar/aa79b02b74f616f8714d51e419bd10de to your computer and use it in GitHub Desktop.
AWS ECS Deregister Task definitions Bulk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ref:
ListTaskDefinitionsCommand
DeregisterTaskDefinitionCommand