That's the thing... It's not unnecessary, while you might consider the subsequent images superfluous because they're incremental, the initial image created when you deploy is not unnecessary. It contains the information that Cloud Functions would need to deploy your code on an instance to then run that code unimpeded, as when you are not calling said function, the instance that runs the code is deleted, and all that's left is the image.
To try and explain myself a little better, I'll try and put the process as a bulletpoint list.
The first time you deploy a function the process is essentially this:
1) Grab existing base image for the appropriate language
2) Use said image to create an instance
3) Apply user code to the instance
4) Save said changes to an image ; create bucket if it does not exist
5) Deploy image with user code to instance; destroy (instance's container) when no longer use
Every time thereafter, the process would go a little something like this:
1) Check for bucket --> If bucket doesn't exist, create it, and go through first-time process
2) If the bucket exists, grab the image --> The image is expected here, if it is not found, it throws an error. At minimum, the initial or latest version should be here
3) Deploy image with usercode to instance
As you might've noticed, every time after the first, the deployment is about half the length as the initial one, if not more (at least in steps).
As for the pricing... Lets start by looking at storage:
https://cloud.google.com/artifact-registry/pricing#storage1GB of storage is... .10$ That is to say 1/10th of a dollar, a little over 8.3 cents of a euro (83/1000ths), 7.36 indr, almost 11 yen (10.9); and that would be every month.
Now, I can perfectly understand that, as much as that may be nothing where I live, it can be expensive or at the very least noticeable to someone else depending on their salary, the cost of living, etc. However, given that functions only have multiple images if you are generating new versions, and it's entirely possible that you may need to revert to a previous version to rollback some unfortunate changes, I would not call this unnecessary storage.
What's more, the stackoverflow link you've sent includes warnings of this exact error (and I'm not sure why one would upvote this kind of workaround when people are having issues with it...) :
"
NOTE: if you face any deployment issues later, like if you deploy several days in a row and if it gives you an error on deploy, just delete the whole "container" folder manually in the artifacts which should solve it and then redeploy again. (make sure not to delete the artifacts bucket itself!)
Hope the firebase team will improve that - the current behavior looks confusing as it easily leads to an unexpected bill unless you take extra steps to prevent that. But you'll never know that it will happen until it does.
"
And while I disagree on the current behaviour "looking confusing", I can understand that our documentation can seem misleading... But anything we obviate is because it's part of the documentation, it's not something we hide or attempt to confuse anyone about. There's clear notifications on a Function's version, and you have the ability to go back to previous versions if you so desire. In fact, removing old versions of functions is quite easy so long as you modify old versions of functions like so:
https://firebase.google.com/docs/functions/manage-functions#modifyAnd rely on implicit deletion (mentioned here:
https://firebase.google.com/docs/functions/manage-functions#delete_functions ) or, alternatively, delete them manually.
PS: This
https://gist.github.com/kichiemon/4ba5bf921bc9e4d208db8723da69f0ed was recommended on
https://issuetracker.google.com/issues/186832976#comment18 ... And the descriptions don't seem to be related... From what I've read of that thread (which is all of it), everyone on it is having different errors, and... Just, don't do this, especially not if you're deleting the images that instances use to run your Functions. I trust the Firebase Team, and I trust that they did such a thing as a last resort, but it has little to do with your case, as it seems that particular commenter's containers were stuck in some way.