-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_user.py
More file actions
54 lines (39 loc) · 1.48 KB
/
delete_user.py
File metadata and controls
54 lines (39 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import psycopg2
import os
from dotenv import load_dotenv
# Load the connection string from .env
load_dotenv()
def force_delete_user():
print("--- ADMIN: FORCE DELETE USER ---")
username = input("Enter the username to delete: ").strip()
if not username:
print("Operation cancelled.")
return
confirm = input(f"⚠️ Are you sure you want to permanently delete '{username}'? (yes/no): ")
if confirm.lower() != 'yes':
print("Operation cancelled.")
return
try:
conn = psycopg2.connect(os.environ.get('DATABASE_URL'))
cur = conn.cursor()
# 1. Get User ID
cur.execute("SELECT id FROM users WHERE username = %s", (username,))
user = cur.fetchone()
if not user:
print(f"❌ User '{username}' not found.")
return
user_id = user[0]
# 2. Delete all passwords belonging to this user (Foreign Key cleanup)
cur.execute("DELETE FROM passwords WHERE user_id = %s", (user_id,))
passwords_deleted = cur.rowcount
# 3. Delete the user record
cur.execute("DELETE FROM users WHERE id = %s", (user_id,))
conn.commit()
print(f"\n✅ SUCCESS: User '{username}' deleted.")
print(f"🗑️ Also removed {passwords_deleted} password entries belonging to them.")
cur.close()
conn.close()
except Exception as e:
print(f"\n❌ ERROR: {e}")
if __name__ == '__main__':
force_delete_user()