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

**URL:** <https://community.homey.app/t/script-mass-delete-all-unavailable-devices-or-broken-flows-at-once/77918>\
**Category:** Tutorials\
**Tags:** homey-pro\
**Created:** [March 17, 2023, 4:50pm UTC](https://community.homey.app/t/script-mass-delete-all-unavailable-devices-or-broken-flows-at-once/77918 "2023-03-17T16:50:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [March 17, 2023, 4:50pm UTC](https://community.homey.app/t/script-mass-delete-all-unavailable-devices-or-broken-flows-at-once/77918/1 "2023-03-17T16:50:27Z")

</div>

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:

 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/c/bc1f64ea210f8eb67f80381fb2282457e916963c.jpeg)

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](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:

```auto
//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](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/e/d/ed25eff5e2fc863c663e6eeddbd63e165082be86.png)

Chrome:  
 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/5/0/503443389029aa3885a8440baa1d44131e6f81ed.png)

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](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/8/6/8688fd2d34520688720196a2362925336d440e9b.png)

And watch the console:

 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/d/0/d00e5968bb8b4752d8966ec3afded0cc3512a69f.jpeg)  
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 👆 :

```auto
//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();

```

---

<div class="post-metadata">

**Author:** ![Arie\_J\_Godschalk](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arie_j_godschalk/32/63027_2.png) [@Arie\_J\_Godschalk](https://community.homey.app/u/Arie_J_Godschalk)\
**Post date:** [March 17, 2023, 6:58pm UTC](https://community.homey.app/t/script-mass-delete-all-unavailable-devices-or-broken-flows-at-once/77918/2 "2023-03-17T18:58:26Z")

</div>

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 😉  
Like my lawnmower, which is currently unavailable.

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [March 17, 2023, 8:10pm UTC](https://community.homey.app/t/script-mass-delete-all-unavailable-devices-or-broken-flows-at-once/77918/3 "2023-03-17T20:10:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Arie\_J\_Godschalk](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arie_j_godschalk/32/63027_2.png) [@Arie\_J\_Godschalk](https://community.homey.app/u/Arie_J_Godschalk)\
**Post date:** [March 17, 2023, 9:38pm UTC](https://community.homey.app/t/script-mass-delete-all-unavailable-devices-or-broken-flows-at-once/77918/4 "2023-03-17T21:38:57Z")

</div>

Here, another script that may help:

> [@\[APP\]\[Pro\] Simple (Sys) LOG - Use this app for Simple (Sys) Logging](https://community.homey.app/t/app-pro-simple-sys-log-use-this-app-for-simple-sys-logging/77533/7):
>
> Script to remove all flowcards in Flows and Advanced Flows from a certain app Use this script to remove all flowcards from a certain app from flows and advanced flows. Lines are removed too, tokens are not. Execute this in the [WebApi Playground](https://tools.developer.homey.app/tools/api-playground). Note: Work in progress. const version = '1.0.5'; //You need to run this script in the API Playground to really remove all flowcards. //const flowNames = ['AddLogCards (Test)', 'DeleteCardsFlow']; // Use this line to run a few select flows by name. …

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [March 18, 2023, 10:59am UTC](https://community.homey.app/t/script-mass-delete-all-unavailable-devices-or-broken-flows-at-once/77918/5 "2023-03-18T10:59:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Arie\_J\_Godschalk](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/arie_j_godschalk/32/63027_2.png) [@Arie\_J\_Godschalk](https://community.homey.app/u/Arie_J_Godschalk)\
**Post date:** [March 18, 2023, 11:51am UTC](https://community.homey.app/t/script-mass-delete-all-unavailable-devices-or-broken-flows-at-once/77918/6 "2023-03-18T11:51:46Z")

</div>

It’s just called AI m8…

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

---

<div class="post-metadata">

**Author:** ![Satoer](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/satoer/32/38113_2.png) [@Satoer](https://community.homey.app/u/Satoer)\
**Post date:** [March 18, 2023, 12:47pm UTC](https://community.homey.app/t/script-mass-delete-all-unavailable-devices-or-broken-flows-at-once/77918/7 "2023-03-18T12:47:57Z")

</div>

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:

 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/9/e/9ebcd1583810a057ebd917d6ba433bd8e825ab87.png)  
It even explains what it is doing.  
And it works:  
 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/8/a/8a09a3934b3ed917adcee018033a6d7e77d6b5aa.png)

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

 ![afbeelding](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/7/e/7eab261750de013ade0633331fe7438533133ff9.png)

[https://chat.openai.com/chat](https://chat.openai.com/chat)

---

<div class="post-metadata">

**Author:** ![Marcel\_Ubels](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marcel_ubels/32/18860_2.png) [@Marcel\_Ubels](https://community.homey.app/u/Marcel_Ubels)\
**Post date:** [March 18, 2023, 1:08pm UTC](https://community.homey.app/t/script-mass-delete-all-unavailable-devices-or-broken-flows-at-once/77918/8 "2023-03-18T13:08:20Z")

</div>

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