Home > Blockchain >  Are files from /tmp automatically deleted by AWS Lambda?
Are files from /tmp automatically deleted by AWS Lambda?

Time:10-25

Does AWS clear the /tmp directory automatically?

Is there a flush() sort of function? If not, how can I remove/delete all the files from the /tmp folder?

I have an AWS Lambda function where I download a file to my /tmp folder. I unzip the zipped file and gzip all individual files. All this happens within the /tmp directory and before I upload the gzipped files to S3 again.

Afterwards, I no longer need the files in my /tmp folder & would like to clear the directory.

If I open /tmp from my local macOS machine, I don't see any related files at all so I am not sure how to check if they are successfully deleted or not.

CodePudding user response:

No, files from /tmp are not automatically deleted.


The AWS Lambda FAQs state:

To improve performance, AWS Lambda may choose to retain an instance of your function and reuse it to serve a subsequent request, rather than creating a new copy. To learn more about how Lambda reuses function instances, visit our documentation. Your code should not assume that this will always happen.

As per the above doc and experience, you may find an empty or "pre-used" /tmp directory depending on if AWS Lambda has reused a previous Lambda environment for your current request.

This may, or may not be suitable depending on the use case & there's no guarantees so if you need to ensure a clean /tmp directory on every function invocation, clear the /tmp directory yourself.


Is there a flush() sort of function?

How to delete all files inside the /tmp directory will be dependent on the Lambda function runtime - AWS does not (& shouldn't) offer a way programmatically via their SDK as this is related to file I/O.

For Python, try:

from subprocess import call
...
call('rm -rf /tmp/*', shell=True)
  • Related