[Script] Mass delete all unavailable devices or broken flows at once

After I set up my new Homey Pro 2023, moved almost all devices from my Homey 2019 and disabled unused apps, it was left like a graveyard:

I wanted to remove all devices that are unavailable, But I need to keep the devices that are still being used. So I wrote a “Web API Playground” script that would do that for me. I guess there are more lazy people like me in the community, so I thought I would share it.

Mass remove all unavailable devices:

First:
USE THIS AT YOUR OWN RISK! I’m not responsible if something goes wrong. Especially with a mass delete script like this.

Go to:
https://tools.developer.homey.app/tools/api-playground
Make sure you have selected the right Homey (Below user name) and replace the example script with this script:

The script:

//FIRST DO A DRY RUN!!
//If you want to delete all unavailable devices change next line to: deleteDevices=true;
var deleteDevices=false;


async function removeUnavailableDevices() {
  // Get all devices
  const devices = await Homey.devices.getDevices();

  // Convert devices object to an array
  const deviceArray = Object.values(devices);
  
  console.log('Devices:');
  
  deviceArray.forEach(device => {

   if (device.available == null) {
     console.log('delete  - ' + device.name);
     if (deleteDevices){
      let deviceId = device.id;
      Homey.devices.getDevice({ id: deviceId })
        .then((device) => {
          Homey.devices.deleteDevice(device)
            .then(() => {
              console.log(`Device with ID ${deviceId} was successfully removed`);
            })
            .catch((err) => {
              console.error(`Failed to remove device with ID ${deviceId}: ${err.message}`);
            });
        })
        .catch((err) => {
          console.error(`Failed to find device with ID ${deviceId}: ${err.message}`);
        });
     }
   }
   else {console.log('keep    - ' + device.name) }
  });
}

removeUnavailableDevices();

Note: This can’t be used inside the HomeyScript environment, because deleting devices require special permissions.

Now right click somewhere inside the browser and select “Inspect”
This will divide the window with developer tools. Open the “Console” tab:

Firefox:
afbeelding

Chrome:
afbeelding

Now, make sure that on top of the script:
var deleteDevices=false;
is false, to do a dry run without actually deleting devices.

Press RUN:
afbeelding

And watch the console:


devices marked with “keep” will NOT be deleted
Devices marked with “delete” WILL be deleted

Happy? now change the top of the script to:
var deleteDevices=true;

Pray to the gods of bit and Byte.
And all your unavailable devices are deleted. Or most of them. I removed ton of devices but some where getting a timeout error. If this occurs and not all devices are deleted, just run it again.

Mass remove all broken flows:

Here is a script to remove all broken flows (flows with missing devices) Same procedure as above :point_up_2: :

//FIRST DO A DRY RUN!!
//If you want to delete all broken flows change next line to: deleteFlows=true;
var deleteFlows=false;

async function deleteBrokenFlows() {
    // Get all flows
    let flowsObj = await Homey.flow.getFlows();

    // Convert the flows object to an array
    let flowsArr = Object.values(flowsObj);

    // Loop through the flows and log their names
    flowsArr.forEach(flow => {
      if (flow.broken) {
        console.log('DELETE - '+ flow.name);
        if (deleteFlows) {
          if (Homey.flow.deleteFlow({id: flow.id})){
            console.log(flow.id+' removed');
          }
        
        }
      } else{   
        console.log('KEEP   - '+ flow.name);
      }  
    });
}

// Call the async function to list the flows
deleteBrokenFlows();
2 Likes

Nice script @Satoer .

Perhaps one suggestion if i may: why not check if the device-app-id is from an app that doesn’t exist?
Then it will not delete devices that are just temporarely unavailable :wink:
Like my lawnmower, which is currently unavailable.

Thanks and good suggestion, but I’m just a hobby programmer and to be honest, most of the script was written with ChatGPT. :laughing:
Also hard to test now I already removed all of my ghost devices. :sweat_smile: It was just a quick solution to save me some time.

1 Like

Here, another script that may help:

Written with ChatGPT. Can you give an example how you can achieve that. Is AI that far that it is able to program?

It’s just called AI m8…

But yeah, the coding of chatGPT is pretty far yeah.

1 Like

It is pretty insane. And it is a really helpful tool. It does not always produce working code, but you can ask to fix problems with it. But I do think you need some basic Javascript knowledge to work with it. I didn’t even knew ChatGPT had Homey knowledge until I tried it.

The way I think it works best, is by sub dividing the program into sub sections. Try to find solutions to sub problems first, and then connect it all together.

Above code was first trying to delete a device on Homey. That had some complications, mainly a permission problem. A helpful developer on slack pointed me to the api-playground that probably could handle it (and it could).

Then I asked to iterate through all devices. It didn’t gave me the exact results I needed, but the ChatGPT example code gave me enough knowledge to fix the problem and connect everything together.

Here is an ChatGPT example where I ask for a HomeyScript that lists all devices, you can see it does not produce working code right away, but after I showed it the error it fixed the code for me:


It even explains what it is doing.
And it works:

Just play with it. It is free. Not only for programming but with almost anything you would like to know:

https://chat.openai.com/chat

4 Likes

Wow i am impressed. Will give it a try. I don’t have much Js expertise, but i am pretty good with copy/paste :joy:

3 Likes