2

I am writing a site that sends confirmation emails on signup. Until the user clicks the link, the is_active field is set to false. How can I automatically delete any users who do not click the activation link within a certain period of time? Is this even a viable option or is there a better way to work around users who never confirmed their accounts?

Thanks for any help!

  • 1
    Are you sure you want to delete them? You could just leave them inactive. Otherwise, you could run a task periodically. – The_Cthulhu_Kid Feb 19 '18 at 04:29
  • is there a certain way to go about doing that? Is there a specific plugin i should look into? – Austin Sutton Feb 19 '18 at 04:33
  • 1
    Have a look at using `celery` or even just a `cron job` would do. There might be a package that does it for you but I am not sure. – The_Cthulhu_Kid Feb 19 '18 at 04:40
  • I had a similar problem, I don't think there is any way to do this from your Django (Unless using a library) so you would need to see how to run a scheduled task with your web hoster. For Pythonanywhere and Heroku this was very easy. – Zeyad Shaban Nov 21 '20 at 05:27
  • **@The_Cthulhu_Kid** How to do this with no celery or cron job, I checked them out, and it seems too complicated. – AnonymousUser Sep 28 '21 at 04:28
  • 1
    @AnonymousUser Typical Web application server runs your application code once an HTTP request is received. To run scheduled tasks outside of request-response pattern, you need a mechanism to invoke your tasks. Cron is a straightforward way to do exactly that. If there just isn't a way to run application code outside of request processing on your platform, it would technically be possible to run maintenance tasks within request processing. It would however be *a lot* more complicated than using cron or even Celery, and it would delay incoming request processing, sometimes significantly. – borellini Jan 25 '23 at 11:54

1 Answers1

0

I had the same problem, what i did was set a celery task that checks for user that hasn't activated their account after a specified period... your task.py should be like this

@shared_task(name="delete_unactivated_users")
def delete_unactivated_users(self, *args, **kwargs):
    User = get_user_model()         # Get User model

    inactive_users = User.objects.filter(
        date_joined__lt=timezone.now() - timezone.timedelta(seconds=86400),
    )       # Queryset to get users that have created an account but didn't activate them in a day.

    for user in inactive_users:
        # Optional line to log deleted users to a file
        deleted_users_log(user)
        user.delete()

function to write to file

def deleted_users_log(user):
    f = open('/deleted_user_log.txt', 'a')
    opened_file = File(f)
    opened_file.write(f"Unactivated user {user.username} with firstName: {user.first_name}, lastName: {user.last_name}, and email: {user.email} have been flushed down the drain at: {datetime.datetime.now().time()}.\n")
    opened_file.close
    f.close

I am assuming that your token will expire after a day (24hrs)(86400 seconds) Reference

Abdul Giwa
  • 73
  • 10