Skip to content

Instantly share code, notes, and snippets.

@pwmcintyre
Last active March 29, 2022 05:44
Show Gist options
  • Save pwmcintyre/d0ce1ed25ea9b521c996dbe1a68ab6d9 to your computer and use it in GitHub Desktop.
Save pwmcintyre/d0ce1ed25ea9b521c996dbe1a68ab6d9 to your computer and use it in GitHub Desktop.
// Example usage:
// $ npm i @aws-sdk/client-glue
// $ DATABASE_NAME=example-database npx ts-node ./purge.ts
import * as AWS from "@aws-sdk/client-glue"
const client = new AWS.Glue({})
const DatabaseName = process.env['DATABASE_NAME']
// begin async
;(async () => {
// get tables
const tables = await client.getTables({ DatabaseName })
console.log("found tables", { DatabaseName, count: tables.TableList?.length })
if ( !tables.TableList ) { return }
// process each table
tables.TableList.forEach( async ({ Name }) => {
// getPartitions doesn't seem to have a paginator... so we just loop until no more partitions
while (true) {
// get partition batch
const partitions = await client.getPartitions({ DatabaseName, TableName: Name, MaxResults: 25 })
console.log("dropping", { DatabaseName, TableName: Name, partitions: partitions.Partitions?.length })
if ( !partitions.Partitions?.length ) { return }
// delete batch
const PartitionsToDelete: AWS.PartitionValueList[] = partitions.Partitions.map( p => ({ Values: p.Values }) )
await client.batchDeletePartition({ DatabaseName, TableName: Name, PartitionsToDelete })
}
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment