1

I have my server side in django with postgres.

What I want to do is to clear a specific table from all of its data at 12:00 (midnight) everyday.

I guess the code for delete will be

Rides.objects.all().delete()

What I am not sure about is how to have a process or sort of a thread that will do it everyday at 12:00.

Also I think maybe the server clock should be set to my TimeZone, to make sure its deleting the data at 12:00 exactly.

How should I go about this? any ideas?

Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101
  • 3
    Its done via the so called [cron jobs](http://stackoverflow.com/questions/573618/django-set-up-a-scheduled-job) – Todor Jan 29 '16 at 11:57
  • "server clock should be set to my TimeZone", the time zone you're in isn't always the same, it shouldn't be hard to figure out what your idea of 12:00 on the server is – Sayse Jan 29 '16 at 12:32

1 Answers1

0

Create management command

Create a file inside the app directory

management/commands/cleanup_rides.py

from django.core.management.base import NoArgsCommand
from models import Ride

class Command(NoArgsCommand):
    help = "cleaning up rides"

    def handle_noargs(self, **options):
        Ride.objects.all().delete()

then add it to the crontab run the command crontab -e add the below line

0 0 * * * path/to/python path/to/project/manage.py cleanup_rides

the managment command runs every day midnight and delete all the records in ride table.

Anoop
  • 2,748
  • 4
  • 18
  • 27
  • thanks for your answer. I did all the above, but when trying to run the command in the project class - I get error: 'crontab' is not recognized as an internal or external command. I guess I need to install something? working in windows 8 – Ofek Agmon Jan 29 '16 at 14:40
  • Oh sorry I thought you are working in linux. The windows equivalent to a cron job is a scheduled task. – Anoop Jan 29 '16 at 15:26
  • Thanks. Eventually I will have this django server running in heroku, I should be able to use crontab with heroku right? – Ofek Agmon Jan 29 '16 at 16:29