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'));