This script is an example of how to use the Snyk API to find and remove all empty targets from a Snyk group. It uses pysnyk to make a list of the orgs in the group, and then makes use of the following APIs to enumerate and delete the targets:
The algorithm used by this example is as follows:
- Get list of orgs
- For each org:
- get all the targets for the org
- get all the non-empty targets for the org (use the "excludeEmpty" filter)
- find the empty targets by comparing the non-empty list with the complete list and using the difference
- add the empty targets to a dictionary of targets keyed by organization ID
- Iterate through the dictionary of empty targets and delete each one
The script outputs a list of the org/targets that were deleted (or
would have been deleted, if omitting --delete
flag) to stdout
, one per line.
Example use:
dry-run:
python3 rm-empty-targets.py $SNYK_GROUP_ID
actual deleting:
python3 rm-empty-targets.py $SNYK_GROUP_ID --delete