Skip to content

Instantly share code, notes, and snippets.

@dt-rush
Last active May 8, 2020 23:13
Show Gist options
  • Save dt-rush/12946b6bb0a20984d52a59e3477d60f2 to your computer and use it in GitHub Desktop.
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
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'));
@dt-rush
Copy link
Author

dt-rush commented May 8, 2020

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 resources
APP: the numeric id of the application (eg. 1330472)

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