Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
How to clean up /tmp files after cloud function sends response?

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;
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!