I have a Google Cloud Run container acting as an express server.
End users upload files into a temporary directory, I process them, and send a file back.
I'd like to clean up the original upload (and other intermediary files) so that they don't take up memory.
But the docs say that code after the response isn't guaranteed to execute.
So how/where do I delete the temp files?
Here's a simplified example:
// process user upload, then:
res.download('someFile');
fs.unlinkSync('someFile') // not guaranteed to run;
return null;
With Cloud Run, you can use the CPU always ON feature. It's not the cheapest, but it specially designed to continue to work in the background, even if there isn't handled request. And that, up to 15 minutes after the latest response send and if there isn't new request on the instance.
If you use Cloud Functions 1st gen, I can recommend you to clean the /tmp dir when you start the processing of your Cloud Functions, not at the end.
Note: Cloud Functions 2nd gen is similar to Cloud Run
EDIT 1
You can play with the system and hack it to implement what you want.
After sending the response back, you can create your background process that delete the specific file in /tmp.
Because you have no longer access to CPU, that instruction will be on hold.
BUT, when the next request is received, the CPU is coming back to life for the whole instance (not for the current request) and therefore your background process will resume and delete the files.
If the instance doesn't receive a new request for the next 15 minutes, it will be offloaded, and the memory (and your files) swiped automatically!