Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8705363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:20:33+00:00 2026-06-13T03:20:33+00:00

I have the following code in one of my models class PostImage(models.Model): post =

  • 0

I have the following code in one of my models

class PostImage(models.Model):
    post =  models.ForeignKey(Post, related_name="images")
    # @@@@ figure out a way to have image folders per user...
    image = models.ImageField(upload_to='images')
    image_infowindow = models.ImageField(upload_to='images')
    image_thumb = models.ImageField(upload_to='images')
    image_web = models.ImageField(upload_to='images')
    description = models.CharField(max_length=100)
    order = models.IntegerField(null=True)

    IMAGE_SIZES = {
        'image_infowindow':(70,70),
        'image_thumb':(100,100),
        'image_web':(640,480),
    }

    def delete(self, *args, **kwargs):
        # delete files..
        self.image.delete(save=False)
        self.image_thumb.delete(save=False)
        self.image_web.delete(save=False)
        self.image_infowindow.delete(save=False)
        super(PostImage, self).delete(*args, **kwargs)

I am trying to delete the files when the delete() method is called on PostImage. However, the files are not being removed.

As you can see, I am overriding the delete() method, and deleting each ImageField. For some reason however, the files are not being removed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:20:34+00:00Added an answer on June 13, 2026 at 3:20 am

    You can delete a model instance with multiple methods.

    One method is by calling delete():

    PostImage.objects.get(...).delete()
    

    In this case the delete() is called, hence the files will be removed. However you can also delete objects by using querysets:

    PostImage.objects.filter(...).delete()
    

    The difference is that using the latter method, Django will delete the objects in bulk by using SQL DELETE command, hence the delete() method for each object is not called. So probably you are deleting objects using querysets, and therefore files are not being removed.

    You can solve this by using post_delete Django signal as follows:

    @receiver(post_delete, sender=PostImage)
    def post_delete_user(sender, instance, *args, **kwargs):
        instance.image.delete(save=False)
        instance.image_thumb.delete(save=False)
        instance.image_web.delete(save=False)
        instance.image_infowindow.delete(save=False)
    

    Please note that if you use this method, you don’t have to overwrite the delete() method anymore.

    More about this here and here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Scala IDE I have the following code: package pkg.one object S { class
I have the following code which swaps out one divs innerHTML for the others
I have a class and it's modelForm. class UserGoal(models.Model): user = models.ForeignKey(User) goal =
I have the following model: class UptimeManager(models.Manager): def with_length(self): Get querySet of uptimes sorted
I have the following multi-table inheritance situation: from django.db import Models class Partner(models.Model): #
I have the following code in one of my models: def shortDescription(self): return self.name
I have the following code in one cpp file which is part of a
I have the following code in one of my Rails 3 application views: <ul>
I have the following code in one of our projects webpages: XmlDocument xDoc =
Using linkedin-j , I have the following code in one part of my application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.