Last active
May 8, 2020 23:13
-
-
Save dt-rush/12946b6bb0a20984d52a59e3477d60f2 to your computer and use it in GitHub Desktop.
Delete balena application resources via node SDK when app delete as a whole is timing out
This file contains hidden or 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 getSdk = require('balena-sdk'); | |
const balena = getSdk({ | |
apiUrl: "https://api.balena-cloud.com/", | |
dataDirectory: "/tmp" | |
}); | |
const key = process.env.BALENA_KEY; | |
const appId = parseInt(process.env.APP, 10); | |
const countResource = (resource) => { | |
return balena.pine.get({ | |
resource: resource + '/$count', | |
options: { | |
$filter: { belongs_to__application: appId } | |
} | |
}); | |
} | |
const deleteBatch = async () => { | |
await balena.auth.loginWithToken(key); | |
const currentRelease = (await balena.models.application.get(appId)).commit; | |
// delete all devices | |
const deviceBatchSize = 10; | |
while (await countResource('device') > 0) { | |
console.log(`deleting ${deviceBatchSize} devices...`); | |
const result = await balena.pine.delete({ | |
resource: 'device', | |
options: { $filter: | |
{ belongs_to__application: appId }, | |
$top: deviceBatchSize } | |
}); | |
} | |
// delete all releases | |
const releaseBatchSize = 100; | |
while (await countResource('release') > 1) { | |
console.log(`deleting ${releaseBatchSize} releases...`); | |
const result = await balena.pine.delete({ | |
resource: 'release', | |
options: { $filter: | |
{ belongs_to__application: appId, commit: { $ne: currentRelease } }, | |
$top: releaseBatchSize } | |
}); | |
} | |
}; | |
deleteBatch().then(() => console.log('completed')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
L6-L7 indicate that one must have the environment variables defined for this to work:
BALENA_KEY
: the API or session token from the dashboard for the user which has auth to delete the app's resourcesAPP
: the numeric id of the application (eg.1330472
)