A schedule automatic delete from Database

55 views
Skip to first unread message

mostwanted

unread,
Jun 9, 2019, 2:47:47 AM6/9/19
to web2py-users
Is there a way to schedule an automatic deleting of content from database on a specified date? I have an application that I want  to delete posts when we reach specified dates. If anyone is familiar with this, your assistance would be greatly appreciated.

Regards

Mostwanted

Lovedie JC

unread,
Jun 9, 2019, 5:41:34 AM6/9/19
to web...@googlegroups.com
Had one, let check my archives. 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/bd76a193-c425-43ca-8a7b-157bd137b860%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lovedie JC

unread,
Jun 9, 2019, 5:47:53 AM6/9/19
to web...@googlegroups.com
import datetime

yesterday = request.now - datetime.timedelta(days=1)
db(db.post.modified_on < yesterday).delete()

I hope this helps. You can work around it but worked for me in deleting previous posts up to the previous date. You don't have to manually delete but works automatically.
Regards

mostwanted

unread,
Jun 10, 2019, 5:16:37 AM6/10/19
to web2py-users
I am having a hard time figuring out how to calculate for variable number of days, in my app i list job posts which have different closing dates, what i want is for the job post to be deleted a day after its closing date. How can achieve this with your algorithm??


On Sunday, June 9, 2019 at 11:47:53 AM UTC+2, Lovedie JC wrote:
import datetime

yesterday = request.now - datetime.timedelta(days=1)
db(db.post.modified_on < yesterday).delete()

I hope this helps. You can work around it but worked for me in deleting previous posts up to the previous date. You don't have to manually delete but works automatically.
Regards

On Sun, 9 Jun 2019 at 12:41, Lovedie JC <lbjc...@gmail.com> wrote:
Had one, let check my archives. 

On Sun, 9 Jun 2019, 09:47 mostwanted <godir...@gmail.com> wrote:
Is there a way to schedule an automatic deleting of content from database on a specified date? I have an application that I want  to delete posts when we reach specified dates. If anyone is familiar with this, your assistance would be greatly appreciated.

Regards

Mostwanted

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.

Lovedie JC

unread,
Jun 10, 2019, 5:23:42 AM6/10/19
to web...@googlegroups.com
Is it on the same table? If yes, then you can select with the ID. In the example above the delete is for a post that has lasted one day. If for example a post lasts 3 days then timedelta(days=3), so you decide on the number of days you want for each post.


To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/4bc7dba8-be9f-465e-958a-0eb1e07f7da7%40googlegroups.com.

Lovedie JC

unread,
Jun 10, 2019, 5:27:27 AM6/10/19
to web...@googlegroups.com
Further more the request.now which works with the time the item is posted can also be variable with a different dates i.e if you want an item posted 1 week ago, you can use the datetime function to select the date one week ago and choose the date of deleting using timedelta(days =number)
Regards 

On Mon, 10 Jun 2019, 12:16 mostwanted <godir...@gmail.com> wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/4bc7dba8-be9f-465e-958a-0eb1e07f7da7%40googlegroups.com.

mostwanted

unread,
Jun 10, 2019, 10:45:04 AM6/10/19
to web...@googlegroups.com
Thanks alot @Lovedie JC, I get it now, i figured out how to use it.

Lovedie JC

unread,
Jun 10, 2019, 10:57:39 AM6/10/19
to web...@googlegroups.com
Welcome 

On Mon, 10 Jun 2019, 17:45 mostwanted <godir...@gmail.com> wrote:
Thanks alot @Lovedie JC, I get now, i figured out how to use it.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/b43774ea-6710-443c-a3d0-aeb3f2a4aa4a%40googlegroups.com.

Dave S

unread,
Jun 10, 2019, 3:59:04 PM6/10/19
to web2py-users

The other posts  cover how to clean things up when there is a relevant request around the time that cleaning is desired.

If you want the cleaning to be done on a regular basis, the Scheduler is the weapon of choice.  There's a good description in the book,  at
<URL:http://web2py.com/books/default/chapter/29/04/the-core#web2py-Scheduler>
and you can find a bunch of posts in the forum archives.

/dps

mostwanted

unread,
Jun 11, 2019, 12:52:59 AM6/11/19
to web2py-users
I am yet to understand how the scheduler works.

Thanks Dave

Dave S

unread,
Jun 11, 2019, 11:20:13 AM6/11/19
to web2py-users


On Monday, June 10, 2019 at 9:52:59 PM UTC-7, mostwanted wrote:
I am yet to understand how the scheduler works.

Thanks Dave


It's simple:  the Scheduler runs as an independent process that watches only one thing:  the list of queued tasks.  When it finds that it is time to run a queued task, it unqueues it and launches it.  A queued task could be one that looks through another table (such as the one used by the web requests), and compares the time of the entries with the current time and decides which ones are stale, and then marks them inactive or deletes them.

Let's say you have a table where users post discussion topics:  perhaps design documents.  The form shown in the browser might include a link to the document, the author's name, the project it applies to, and the date posted.  You also have a field (hidden in the form) for a status of active/inactive/removed.  The removed status is for when the author cancels a document, so we won't worry about that during cleanup.

You want to have the table checked every day at 11:59 pm, and all the active documents older than 2 weeks to be marked inactive.  You set up the Scheduler in your model file, and define a task; that is, you write a function to implement the task (more below).  You start the Scheduler from a command line (bash shell, or on Windows a cmd window).  You also write a small function in one of your controllers that has the scheduler.queue_task() call setting the start time and unlimited repeats and how often it repeats (every 24*60*60 seconds in this case).  You invoke that function (from a browser if you want it to be a web request, from a command line using -S if you want it to be only done from the server environment.

The function you wrote to implement the task is very simple:  select all the documents that are active and are 2 weeks old (post_date.< now() - datetime.delta(2 weeks)) and update the status to inactive.

Also note that recent versions of the scheduler allow you to do a recurring task with a string that looks like a traditional unix/linux cron entry, rather than calculating the number of seconds between runs.

/dps

Dave S

unread,
Jun 11, 2019, 11:23:57 AM6/11/19
to web2py-users


On Tuesday, June 11, 2019 at 8:20:13 AM UTC-7, Dave S wrote:


On Monday, June 10, 2019 at 9:52:59 PM UTC-7, mostwanted wrote:
I am yet to understand how the scheduler works.

Thanks Dave


It's simple:  the Scheduler runs as an independent process that watches only one thing:  the list of queued tasks.  When it finds that it is time to run a queued task, it unqueues it and launches it.  A queued task could be one that looks through another table (such as the one used by the web requests), and compares the time of the entries with the current time and decides which ones are stale, and then marks them inactive or deletes them.

Let's say you have a table where users post discussion topics:  perhaps design documents.  The form shown in the browser might include a link to the document, the author's name, the project it applies to, and the date posted.  You also have a field (hidden in the form) for a status of active/inactive/removed.  The removed status is for when the author cancels a document, so we won't worry about that during cleanup.

You want to have the table checked every day at 11:59 pm, and all the active documents older than 2 weeks to be marked inactive.  You set up the Scheduler in your model file, and define a task; that is, you write a function to implement the task (more below).  You start the Scheduler from a command line (bash shell, or on Windows a cmd window). 

Using the -K option. 

Or you start the scheduler as a service, basically having systemctld or nssm or such run a command line with the -K option.

mostwanted

unread,
Jun 11, 2019, 12:01:02 PM6/11/19
to web2py-users
Thanks for the elaborate explanation Dave, really means a-lot.
Reply all
Reply to author
Forward
0 new messages