Member delete API calls responds with 200 but doesnt delete

Hi there,

So I have embarked on a project to remove all nodes from a network. The only way to do this it seems is by use of the API.

I wrote out an API calls to get all the node information for each member and got a count just shy of 8500.
Ok, thats a lot, no worries. Whats the rate limit? for me 20 API calls per second. Ok so limiting it to 1 API call a second is more than sufficient, cool.

If I select a nodeID, and manually call a deletion… Woo it deletes.
So the premise and theory check out. So how do I delete them all, I would need to loop through all 8500 members and call a deletion.

Well here we go

# Replace NETWORK_ID with the ID of the network you want to remove nodes from
# Replace AUTH_TOKEN with your auth token
$networkID = "PUT NETWORK ID HERE"
$authToken = "PUT AUTHTOKEN HERE"

$headers = @{
    "Authorization" = "Bearer $authToken"
}

# Get a list of all nodes in the network
$nodes = (Invoke-WebRequest -Uri "https://api.zerotier.com/api/v1/network/$networkID/member" -Headers $headers).Content | ConvertFrom-Json

$nodes = $nodes | sort-object -Property lastOnline
# Loop through each node and remove it from the network
foreach ($node in $nodes) {
    Invoke-WebRequest -Uri "https://api.zerotier.com/api/v1/network/$networkID/member/$($node.id)" -Method DELETE -Headers $headers
    start-sleep 1
}

Left it running overnight… still shy of 8500.

I really don’t get it.

Has anyone else run into this problem?

p.s. Its Powershell

Is your network set to Public access? Deletion is not possible from public networks, since as soon as the client tries to join again it would just come back otherwise.

It’s set to private as I considered that it would be exactly as you described if not.

The odd thing is that it allows me to delete individual records, but not when I loop it through.

Ok I found the problem. Took a good night’s sleep to find it.

The API URI does not like arrays, or even pulling data from an array within a URI. You have to feed it an absolute.

So here is the updated code for anyone that wants to use it.

$networkID = "NETWORK ID HERE"
$authToken = "AUTHTOKEN HERE"

$headers = @{
    "Authorization" = "Bearer $authToken"
}

# Get a list of all nodes in the network
$nodes = (Invoke-WebRequest -Uri "https://api.zerotier.com/api/v1/network/$networkID/member" -Headers $headers).Content | ConvertFrom-Json

# Loop through each node and remove it from the network
foreach ($node in $nodes) {
    $target = $node.nodeId
    Invoke-WebRequest -Uri "https://api.zerotier.com/api/v1/network/$networkID/member/$target" -Method DELETE -Headers $headers
    #start-sleep 0.5
}

The major change here… instead of specifying that a variable is an absolute once populated, make a variable with nothing but the info you need in it.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.