[Django]-How can I delete duplicated users in django?

3👍

RZs answer is actually nearly correct. I don’t know if it’s the best way, but it works. So for this one time purpose you can use it.

However, I would like to add and correct some stuff.

from django.contrib.auth.models import User

def delete_duplicate_users():
  // first find all email addresses (with kind of a 'group by')
  emails = User.objects.values('email').distinct()

  for e in emails:
    users = User.objects.filter(email=e['email']).order_by('date_joined')[1:]
    for u in users:
      u.delete()

I tried this with a small example and it worked. But I highly recommend that you test this before you actually use it on your production system!

Hope that helps you.

// Edit

I also would recommend that you don’t allow adding users if the email is already registered. There should be some built in method to achieve this. And if not you could subclass the Djangos User model with you own User model and override the save method.

👤Jens

1👍

users = User.objects.filter(email='c@c.com').order_by('join_date')[1:]
for u in users:
    u.delete()

I forget if querysets supports slicing like above and can’t test right now. If they don’t you just need to extract the first element and delete the rest.

👤rz.

0👍

You can get the email addresses like this.

from django.contrib.auth.models import User
from django.db.models import Count

duplicate_emails = [i['email'] for i in User.objects.values('email').annotate(
    Count('email')).filter(email__count__gt=1)]

Then you can loop through the email addresses and decide what to do with them. This example deletes the user with an older last_login date.

for email in duplicate_emails:
    user = User.objects.filter(email=email).order_by('last_login')[0]
    user.delete()

Leave a comment