Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Django deletes uploaded files from the temporary directory?

According to the docs:

However, if an uploaded file is too large, Django will write the uploaded file to a temporary file stored in your system’s temporary directory. On a Unix-like platform this means you can expect Django to generate a file called something like /tmp/tmpzfp6I6.upload. If an upload is large enough, you can watch this file grow in size as Django streams the data onto disk.

So I have uploaded several times the same big file to a view and got this:

vic@vic /tmp $ ls -l tmp*.upload
-rw------- 1 vic vic2 3436110 мая   14 18:31 tmpKdZrQl.upload
-rw------- 1 vic vic2 3436110 мая   14 18:31 tmpqLyBsy.upload

When these files are deleted from the temp directory?

I want to avoid bloating the temp directory.

like image 555
warvariuc Avatar asked May 14 '14 14:05

warvariuc


1 Answers

To write these tempfiles, Django uses the tempfile module in python. You can see django's code in django/core/files/temp.py.

A django temp file is a tempfile.NamedTemporaryFile. Here's a link to the code. The default settings in that constructor (plus code that's in django) calls for the file to be deleted when it is closed.

So watch out for filehandle links; if they're not getting deleted, it is likely that your code is leaking the filehandle, and never closing it.

like image 159
FrobberOfBits Avatar answered Sep 21 '22 16:09

FrobberOfBits