# Auto deleting model instance

**URL:** <https://forum.djangoproject.com/t/auto-deleting-model-instance/13335>\
**Category:** Forms & APIs\
**Created:** [April 23, 2022, 1:46pm UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335 "2022-04-23T13:46:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![osamamhd](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/osamamhd/32/4388_2.png) [@osamamhd](https://forum.djangoproject.com/u/osamamhd)\
**Post date:** [April 23, 2022, 1:46pm UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/1 "2022-04-23T13:46:23Z")

</div>

I have this model:

```auto
class FileUpload(models.Model):
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, null=True,
                              blank=False, on_delete=models.SET_NULL)
    filename = models.CharField(max_length=255)
    upload_datetime = models.DateTimeField()
    expiary_datetime = models.DateTimeField()
    file_content = models.FileField(upload_to='uploads/')

    def __str__ (self):
        return self.filename

```

and I want to delete the object automatically when the file date expires.

---

<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:** [April 23, 2022, 1:48pm UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/2 "2022-04-23T13:48:13Z")

</div>

You would need to create a scheduled task to do this on a periodic basis. I would suggest writing a custom management command that can be scheduled with cron.  
Note that you are also responsible for deleting the file itself as well. Deleting a model having a FileField does **not** remove the underlying file from the file system.

---

<div class="post-metadata">

**Author:** ![osamamhd](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/osamamhd/32/4388_2.png) [@osamamhd](https://forum.djangoproject.com/u/osamamhd)\
**Post date:** [April 23, 2022, 2:01pm UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/3 "2022-04-23T14:01:10Z")

</div>

I used cron, here how I used it:  
I wrote a manager for FileUpload model

```auto
class FileManager(models.Manager):
     def get_queryset(*args, **kwargs):
        return super().get_queryset(*args, **kwargs).filter(
            expiary_datetime__gt=Now()
        )

```

then in cron.py:

```auto
def delete_expired_files():
    FileUpload._base_manager.filter(expiary_datetime__lte=Now()).delete()

```

but this line of code:

```auto
FileUpload._base_manager.filter(expiary_datetime__lte=Now()).delete()

```

works from the shell.

---

<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:** [April 23, 2022, 2:30pm UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/4 "2022-04-23T14:30:54Z")

</div>

Sorry, I guess I wasn’t sufficiently clear.

You need to run this as a custom django-admin management command - see [How to create custom django-admin commands | Django documentation | Django](https://docs.djangoproject.com/en/4.0/howto/custom-management-commands/#module-django.core.management)

You will run `manage.py` in your cron job to run this new command you will create.

---

<div class="post-metadata">

**Author:** ![osamamhd](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/osamamhd/32/4388_2.png) [@osamamhd](https://forum.djangoproject.com/u/osamamhd)\
**Post date:** [April 23, 2022, 2:43pm UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/5 "2022-04-23T14:43:13Z")

</div>

I couldn’t wrap my head around the issue do you have any example for me\>?

---

<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:** [April 23, 2022, 2:48pm UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/6 "2022-04-23T14:48:29Z")

</div>

I’m not sure what “issue” you’re referring to here.

If you’re looking for example management commands, all the standard ones are in your Django installation.

For example, you have probably run the `makemigrations` command. That file, `makemigrations.py` is located in `django.core.management.commands`. All the standard manage commands are in different directories under `django`. You can learn a lot by how those various commands are written.

---

<div class="post-metadata">

**Author:** ![osamamhd](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/osamamhd/32/4388_2.png) [@osamamhd](https://forum.djangoproject.com/u/osamamhd)\
**Post date:** [April 23, 2022, 9:59pm UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/7 "2022-04-23T21:59:40Z")

</div>

I made management command called ‘delete’  
and I used crontab to run the command but won’t run:  
here cronjobs in settings.py:

```auto
CRONJOBS = [
    ('*/1 * * * *', 'django.core.management.commands', ['delete']),
]

```

---

<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:** [April 23, 2022, 10:39pm UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/8 "2022-04-23T22:39:41Z")

</div>

How do you run _any_ management command? (e.g. How do you run ‘makemigrations’?)

---

<div class="post-metadata">

**Author:** ![osamamhd](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/osamamhd/32/4388_2.png) [@osamamhd](https://forum.djangoproject.com/u/osamamhd)\
**Post date:** [April 23, 2022, 11:32pm UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/9 "2022-04-23T23:32:04Z")

</div>

by run:  
`python manage.py makemigrations`

so I need crontab to run:  
`python manage.py delete`  
every minute?!

---

<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:** [April 24, 2022, 1:12am UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/10 "2022-04-24T01:12:53Z")

</div>

If you really need to delete the files every minute, yes.

However, I would find it highly unusual that you would need to do that to that level of granularity. What would be wrong with deleting them once / hour or even once / day?

If you needed to ensure that they weren’t retrieved during the window between when they expire and when they’re deleted, you can modify your model manager to filter queries by expiry time.

And again, don’t forget that deleting the model instance does **not** delete the actual file. If someone with permission to that file knows the url, they would still be able to retrieve it even if the model instance is deleted.

---

<div class="post-metadata">

**Author:** ![osamamhd](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/osamamhd/32/4388_2.png) [@osamamhd](https://forum.djangoproject.com/u/osamamhd)\
**Post date:** [April 24, 2022, 1:39am UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/11 "2022-04-24T01:39:25Z")

</div>

I added this method to FileUplaod model:

```auto
 def delete(self, *args, **kwargs):
        storage, path = self.file_content.storage, self.file_content.path
        super(FileUpload, self).delete(*args, **kwargs)
        storage.delete(path)

```

and I want to run every minute to check if it’s work or not!!  
it didn’t by the way, but when I run : `python manage.py crontab run #job_hash` it works fine.

---

<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:** [April 24, 2022, 1:41am UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/12 "2022-04-24T01:41:08Z")

</div>

There is nothing _within_ Django to run tasks periodically. That’s why this type of task needs an external scheduler - either cron or something like Celery Beats.

---

<div class="post-metadata">

**Author:** ![DobbyWinky](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dobbywinky/32/12573_2.png) [@DobbyWinky](https://forum.djangoproject.com/u/DobbyWinky)\
**Post date:** [March 8, 2023, 4:41am UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/13 "2023-03-08T04:41:52Z")

</div>

Hi,

I followed your steps above. My management command to run everyday was to delete models older than a month.

But cron deamon does not seem to resolve the postgres host.

The same python manage.py \<command\_name\> works perfectly from a terminal but errors out with “could not translate host name “postgres” to address: Temporary failure in name resolution”. Can you please help here

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:** [March 8, 2023, 5:28am UTC](https://forum.djangoproject.com/t/auto-deleting-model-instance/13335/14 "2023-03-08T05:28:52Z")

</div>

I’d need to see the details of the various parts of this.

That would be the cron command being run, your database settings in your settings file, any details about how you’re doing your configuration such as using environment variables, and things like that.

In general, these types of problems are caused because the environment created for commands run by cron are not run in a typical login shell. You don’t have the same environment set by default, and you’re frequently running this as a different user. So the key to finding out where it’s failing is to see where an assumption is being made when you’re running this from your shell that you haven’t accounted for in your cron job.
