When passing an index on the CLI (or in a script) directly as a numerical value it gets translated into an index, but when you’re using a variable, it expects it to already be in the right format. When manipulating items in a script, first fetch their IDs (usually via find). Shouldn’t make much of a difference for your scripting needs, really - if you want to manipulate a rule with a particular offset to 0, you might as well hard code that right into the statement rather that use a variable.
I have found another way of doing it. Though was making sure that I had not missed something as there were other scripts on the Wiki that did something similar that also did not work.
You have to find the records you want to remove first. Find them by anything you wish, comments, in-interface, out-interface, ip addresses, etc - any combination basically to select what you want removed.
print doesnt work* in scripts, there is no buffer to hold the printed lists indexes to use in the next command.