Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save escattone/4e353404faae10dae2fff8754b292fe3 to your computer and use it in GitHub Desktop.
Save escattone/4e353404faae10dae2fff8754b292fe3 to your computer and use it in GitHub Desktop.
def delete_user(user):
# Protected references to users need to be manually deleted first.
user.key_set.all().delete()
# Some records are worth keeping prior to deleting the user
# but "re-assign" to the anonymous user.
anon, _ = User.objects.get_or_create(username="Anonymous")
user.revisionakismetsubmission_set.update(sender=anon)
user.documentdeletionlog_set.update(user=anon)
user.documentspamattempt_set.update(user=anon)
user.documentspam_reviewed.update(reviewer=anon)
user.bans.update(user=anon)
user.bans_issued.update(by=anon)
user.delete()
persona_users = list(User.objects.filter(socialaccount__provider='persona').exclude(socialaccount__provider='github').exclude(socialaccount__provider='google'))
print(f"found {len(persona_users)} persona-only users")
num_deleted = 0
for user in persona_users:
if Revision.objects.filter(creator=user).exists() or AttachmentRevision.objects.filter(creator=user).exists():
continue
try:
delete_user(user)
except Exception as e:
print(f"error while deleting {user.username} ({e})")
else:
num_deleted += 1
print(f"deleted {num_deleted} persona-only users")
@escattone
Copy link
Author

I loaded this within a Django shell (./manage.py shell_plus) on a production web pod to remove all MDN Persona-only users who had made no contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment