-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(scripts): add script for deleting old users that never logged a…
…ny data
- Loading branch information
Showing
2 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import boto3 | ||
from boto3.dynamodb.conditions import Key | ||
from datetime import datetime, timedelta, timezone | ||
|
||
USER_POOL_ID = "us-east-1_rdB8iu5X4" | ||
|
||
events_table = boto3.resource("dynamodb").Table("moodtracker_events") | ||
cognito_idp_client = boto3.client("cognito-idp") | ||
|
||
users_to_delete = [ | ||
u | ||
for u in cognito_idp_client.get_paginator("list_users") | ||
.paginate( | ||
UserPoolId=USER_POOL_ID, | ||
AttributesToGet=[], | ||
) | ||
.build_full_result()["Users"] | ||
if u["UserLastModifiedDate"] < datetime.now(timezone.utc) - timedelta(365) | ||
and not events_table.query( | ||
KeyConditionExpression=Key("userId").eq(u["Username"]), | ||
Limit=1, | ||
)["Count"] | ||
] | ||
print("Users to delete:", users_to_delete) | ||
|
||
if input('Type "yes" to proceed or anything else to abort: ') == "yes": | ||
print("Deleting users...") | ||
for user in users_to_delete: | ||
cognito_idp_client.admin_delete_user( | ||
UserPoolId=USER_POOL_ID, Username=user["Username"] | ||
) | ||
print("Deleted user ID: ", user["Username"]) | ||
else: | ||
print("Aborted") |