Why are all Google Cloud Function source files replicated for every function deployment?

I have noticed through the Firebase Console that my gcf-sources buckets (one for each cloud function region) are taking a lot of storage space. (Around 2GB, when my functions folder is only 47Mb).

After digging into one of the buckets, I discovered that every function deployed has 2 folders (presumably for the latest and latest -1 versions). Inside these folders is a .zip file which contains the ENTIRE firebase functions folder. All of the script files, the full node_modules folder etc.

enter image description here

enter image description here

enter image description here

As you can imagine, this adds up very fast and 2GB is understandable.

My questions are:

  1. Why does every function need a zip file containing the entire functions directory?
  2. Can I safely delete all of this?
  3. How can I prevent this from happening in the future?

For a bit more context, I have done full and partial function deploys, but seem to have the same outcome either way.

Note - I came across this problem after following a previous question to clear up my artefacts folders: How to delete outdated Firebase Cloud function containers from GC Storage?


To understand why, you need to know the process. Your code is uploaded to Cloud Storage and a container is built based on your code (through Cloud Build and Buildpack). Each Cloud Functions version is independent and immutable, therefore there is no code sharing, each time you deploy a function, you deploy the WHOLE code inside that function and the container.

Can you delete those directory? Yes you can. After the deployment, the source file are useless on the bucket. You can set a lifecycle on the bucket if you want and to delete the file older than 1 day. Like that you keep your directory clean.

Be careful, I talk about ONLY the gcf source bucket!! Because the containers are stored in the artifact registry and if you delete the bucket registry you now longer able to get your container (and your code).