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:
Chrome:
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:
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 :
//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();