It's looping through the messages and attachments fine, and I guess I could use a different statement to loop. But the problem is that it's not actually deleting the attachments like it's supposed to. On some further investigation I found that I was not saving the message after I modified it to get rid of the attachment, so I added
after the delete operation, but still the same result.