Last active
May 8, 2020 23:13
-
-
Save dt-rush/12946b6bb0a20984d52a59e3477d60f2 to your computer and use it in GitHub Desktop.
Revisions
-
dt-rush revised this gist
May 8, 2020 . No changes.There are no files selected for viewing
-
dt-rush created this gist
May 8, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ 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'));