One concern with this method is it could catch objects with "TEST" in the name.
I don't know of a way to go from a tag to the objects tagged with it. "where-used" doesn't work for tags:
[Expert@DallasSA]# mgmt_cli -f json -r true show host name staxBerlinDevWebVip | jq '{uid:.uid,name:.name,tags:[.tags[]|{uid:.uid,name:.name}]}'
{
"uid": "70b131cb-e099-4b81-9345-53e5f4752b98",
"name": "staxBerlinDevWebVip",
"tags": [
{
"uid": "962a5a68-29a9-4407-921a-b774890b0a39",
"name": "Development"
},
{
"uid": "0a4b231c-bfad-4b37-8b50-b21a0c0f3f4a",
"name": "Berlin"
},
{
"uid": "63599767-883e-47aa-a04c-fc7d30b16a84",
"name": "Stax"
}
]
}
[Expert@DallasSA]# mgmt_cli -f json -r true where-used uid 962a5a68-29a9-4407-921a-b774890b0a39
{
"code" : "generic_err_object_not_found",
"message" : "Requested object [962a5a68-29a9-4407-921a-b774890b0a39] not found"
}
I think the only guaranteed option is to enumerate all of your objects, find the ones with the tag you care about, then send them to mgmt_cli delete-<type>. Something like this:
TagName="Development"
mgmt_cli -r true login > session.txt
echo "" > toRemove.txt
ObjectCount=$(mgmt_cli -f json -s session.txt show objects limit 1 details-level uid | jq '.total')
for GetOffset in $(seq 1 500 "${ObjectCount}"); do
mgmt_cli -f json -s session.txt show hosts limit 500 offset "${GetOffset}" details-level full | jq -c '.objects[]|{uid:.uid,type:.type,tag:.tags[]|.name}' | grep "\"tag\":\"${TagName}\"" >> toRemove.txt
done
PublishEvery=100
ChangeCount=1
function deleteObject {
mgmt_cli -s session.txt delete-"$1" uid "$2"
((ChangeCount+=1))
if [ ${ChangeCount} -gt ${PublishEvery} ]; then
mgmt_cli -s session.txt publish
ChangeCount=1
fi
}
while read ObjectToRemove; do
deleteObject "$(echo $ObjectToRemove | cut -d '"' -f 8)" "$(echo $ObjectToRemove | cut -d '"' -f 4)"
done < toRemove.txt
mgmt_cli -s session.txt publish
mgmt_cli -s session.txt logout
rm toRemove.txt
rm session.txt
Note that I have not tested this! It should work, though. If you only run the top section (everything before "PublishEvery=100"), it will spit out a file with the objects it would remove. That would let you preview what the bottom section would do.
Make and test a backup before running code you get like this from somebody you don't know.