Spotlight is indexing my external drives even after generating .metadata_never_index
I have a script that reformats an external drive as HFS+, then it disables journaling on the drive, then it disables spotlight indexing via creating a file named ".metadata_never_index" at the root level.
Then the script looks up the amount of free bytes on the drive, divides by 25 megabytes, and loads that many *exactly* 25 MB files on to the drive.
Doing this should leaves me with anywhere between 24.9999999 MB to 0 bytes on the drive.
What actually happens is that every single time I run this on the same drive, I get a different amount of free space left. Sometimes theres no free space left, and the task runs out of space before it can generate all of the 25 MB files.
I don't know if this is the entire problem, but a big part of the problem is that despite creating the .metadat_never_index files, I still get spotlight indexes on the drive. Right now for example, the last time I ran this script on a 250 GB drive, it was unable to finish and when it stopped, it had almost 600 MB in .Spotlight-V100
Now I do have a separate "buffer" amount I subtract from the total free space, before I divide by 25. But sometimes it's not enough because I'm always getting different end results from doing the exact same task over and over. Every last file is 25MB to the byte (except in instances where the drive fills up and it cant finish).
So my primary question is, how else can I truly stop spotlight indexing, from the drive level.
And the second question is, more generally, how can I get consistent results here? I'm going to keep doing more tests and logging the results to try to see if the problem is ALWAYS just the spotlight indexes, or if there are other things that are not adding up, size-wise.
Mac mini, macOS 12.7