# Correct way to handle "orphaned" image files and other files?

**URL:** <https://forum.djangoproject.com/t/correct-way-to-handle-orphaned-image-files-and-other-files/11903>\
**Category:** Using Django\
**Created:** [January 31, 2022, 4:15pm UTC](https://forum.djangoproject.com/t/correct-way-to-handle-orphaned-image-files-and-other-files/11903 "2022-01-31T16:15:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nemecek\_f](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nemecek_f/32/668_2.png) [@nemecek\_f](https://forum.djangoproject.com/u/nemecek_f)\
**Post date:** [January 31, 2022, 4:15pm UTC](https://forum.djangoproject.com/t/correct-way-to-handle-orphaned-image-files-and-other-files/11903/1 "2022-01-31T16:15:18Z")

</div>

Hello,

my project is quite heavy on use-uploaded images and files in general. I am using the `StdImageField` and `FileField`.

What currently the best/correct way to handle these files? I would ideally delete them since they are no longer needed and storage isn’t infinite.

I have found a couple of packages (like [https://github.com/un1t/django-cleanup](https://github.com/un1t/django-cleanup)) but I am bit hesitant using something like this which automatically deletes files.

Other approaches seem to be delete the file when the object is deleted. Something like this:

```auto
image_path = image.image.path

image.delete()

if os.path.exists(image_path):
    os.remove(image_path)

```

With this there is the issue that the image variants created by `StdImageField` will remain on the disk. On the other hand writing logic to similarly delete two additional files doesn’t seem too problematic.

Are there any hidden pitfalls with the approach above? My models that contain the images are designed just for create and delete, existing model is never updated with another image file.

PS: I also got a report that someone uploaded new image with same filename as old one and the app was showing the old image instead of new. Is this possible? In my testing Django appends random chars to the end of the filename when there is a clash.

Many thanks

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 31, 2022, 4:54pm UTC](https://forum.djangoproject.com/t/correct-way-to-handle-orphaned-image-files-and-other-files/11903/2 "2022-01-31T16:54:39Z")

</div>

> [@nemecek\_f](#):
>
> Are there any hidden pitfalls with the approach above? My models that contain the images are designed just for create and delete, existing model is never updated with another image file.

There are no hidden issues that I’m aware of in that situation.

If I remember correctly, the reason why Django doesn’t perform that sort of cleanup is that it can’t be sure that there aren’t other models referring to the same file, or even other systems or environments potentially using those files.

> [@nemecek\_f](#):
>
> I also got a report that someone uploaded new image with same filename as old one and the app was showing the old image instead of new. Is this possible?

Couple different possibilities come to mind:

- Cacheing of the image in the browser
- Cacheing of the html with the old link
- Internal Django cacheing of the links
- The wrong url being exposed in the template

Would definitely need more details to identify a specific cause.

---

<div class="post-metadata">

**Author:** ![nemecek\_f](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nemecek_f/32/668_2.png) [@nemecek\_f](https://forum.djangoproject.com/u/nemecek_f)\
**Post date:** [January 31, 2022, 6:28pm UTC](https://forum.djangoproject.com/t/correct-way-to-handle-orphaned-image-files-and-other-files/11903/3 "2022-01-31T18:28:14Z")

</div>

> [@KenWhitesell](#):
>
> other models referring to the same file

This can happen?

Like a sort of optimization to not duplicate the same files?

I guess unless I setup things this way, it should happen automatically?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 31, 2022, 6:49pm UTC](https://forum.djangoproject.com/t/correct-way-to-handle-orphaned-image-files-and-other-files/11903/4 "2022-01-31T18:49:06Z")

</div>

> [@nemecek\_f](#):
>
> This can happen?

Accidentally, no. Intentionally, yes.

> [@nemecek\_f](#):
>
> Like a sort of optimization to not duplicate the same files?

I could see that being one possible cause, yes.

> [@nemecek\_f](#):
>
> I guess unless I setup things this way, it should happen automatically?

I believe you meant to say “shouldn’t” here instead of “should”? If so, then yes, I agree with you completely.

For example, imagine two completely different Django installations. One, customer facing, allowing users to upload files to some shared storage medium. (Perhaps an AWS S3 bucket, but the specifics do not matter for this example.)  
Now imagine a second system, providing a “file-manager”-style view of that shared storage, where periodically the system scans the storage to find the files located in it - building FileField objects from its discovery process.  
Allowing either of those systems to delete files is potentially problematic.  
(Not that I will ever admit to having seen anything like this)

I’m not saying it’s a good thing to do, or that I would recommend it under most circumstances.

However, Django (among many other web frameworks) will let you make unwise choices.

---

<div class="post-metadata">

**Author:** ![nemecek\_f](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nemecek_f/32/668_2.png) [@nemecek\_f](https://forum.djangoproject.com/u/nemecek_f)\
**Post date:** [January 31, 2022, 7:29pm UTC](https://forum.djangoproject.com/t/correct-way-to-handle-orphaned-image-files-and-other-files/11903/5 "2022-01-31T19:29:54Z")

</div>

Thanks for the detailed answers Ken!

I will implement this manual deletion and see how it goes.

---

<div class="post-metadata">

**Author:** ![karolyi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/karolyi/32/23765_2.png) [@karolyi](https://forum.djangoproject.com/u/karolyi)\
**Post date:** [November 21, 2025, 7:19pm UTC](https://forum.djangoproject.com/t/correct-way-to-handle-orphaned-image-files-and-other-files/11903/6 "2025-11-21T19:19:00Z")

</div>

For anyone hitting this problem, I’ve bumped into it as well, and came up with my solution. Already linked elsewhere, linking it here too for others.

[https://ksol.io/en/blog/articles/2025/11/django-mediafile-deletion/](https://ksol.io/en/blog/articles/2025/11/django-mediafile-deletion/)
