Home Features Philosophy Docs Blog Errors Security Examples FAQ
DJE-023 Warning Serialization

Upload cleanup fails silently

Error message

Temporary upload files accumulate on disk without cleanup

When a LiveView handles file uploads, temporary files are created on the server. If the WebSocket disconnects before the upload is finalized (e.g., user navigates away), the cleanup callback may not fire, leaving orphaned temporary files on disk.

cleanup silent-failure uploads

Affected versions: >=0.2.0

Solution

Before (problematic)
# No cleanup — files accumulate forever
class UploadView(LiveView):
    def handle_upload(self, file):
        path = f"/tmp/uploads/{uuid4()}"
        with open(path, "wb") as f:
            f.write(file.read())
After (fixed)
import os
import time
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = "Clean up orphaned upload temp files"

    def handle(self, *args, **options):
        upload_dir = "/tmp/uploads"
        max_age = 3600  # 1 hour
        now = time.time()
        for fname in os.listdir(upload_dir):
            fpath = os.path.join(upload_dir, fname)
            if now - os.path.getmtime(fpath) > max_age:
                os.remove(fpath)
                self.stdout.write(f"Removed {fpath}")