@acrumley i don’t see your complete code, may be you can post your complete code then will dig more.
but you see the samples from other user and fix.
you can handle by plan also. no need to api method.
samples here : How to remove (de-assign) a role from identity via the API - Compass