Skip to content

Instantly share code, notes, and snippets.

@dt-rush
Last active May 8, 2020 23:13
Show Gist options
  • Select an option

  • Save dt-rush/12946b6bb0a20984d52a59e3477d60f2 to your computer and use it in GitHub Desktop.

Select an option

Save dt-rush/12946b6bb0a20984d52a59e3477d60f2 to your computer and use it in GitHub Desktop.

Revisions

  1. dt-rush revised this gist May 8, 2020. No changes.
  2. dt-rush created this gist May 8, 2020.
    42 changes: 42 additions & 0 deletions delete-app.js
    Original 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'));