agent: |
y4WjjKrw5sIaPIG4GKBTDelete Users in Microsoft Entra ID (Azure AD)
Delete Users in Microsoft Entra ID (Azure AD)
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task permanently removes user accounts from Azure Active Directory. This is crucial for ensuring former employees or associates no longer have access to organizational resources. It helps maintain security by preventing unauthorized access to services and applications linked to Azure AD.
inputs
outputs
from azure.identity import DefaultAzureCredential
from msgraph.core import GraphClient
# Initialize the DefaultAzureCredential and GraphClient
credential = DefaultAzureCredential()
client = GraphClient(credential=credential, scopes=['https://graph.microsoft.com/.default'])
'''
inactive_users = {
'@odata.context': 'https://graph.microsoft.com/v1.0/$metadata#users(displayName,userPrincipalName,signInActivity)',
'value': [
{'displayName': 'test-user', 'userPrincipalName': 'test-user@yashyadav34gmail.onmicrosoft.com', 'id': '9b8d2682-d8c8-41ac-b2dd-3bc0552b4cc9'}
]
}
'''
def delete_user(user_id):
try:
response = client.delete(f'/users/{user_id}')
return response.status_code == 204
except Exception as e:
print(f"An unexpected error occurred while deleting user {user_id}: {e}")
return False
# Check if there are any inactive users to process
if not inactive_users.get('value'):
print("No inactive users provided for processing.")
else:
deletion_count = 0
total_users = len(inactive_users['value'])
for user in inactive_users['value']:
user_id = user.get('id')
display_name = user.get('displayName', 'N/A')
upn = user.get('userPrincipalName', 'N/A')
print(f"Processing deletion for user: {display_name} ({upn})")
# Delete the user account
if delete_user(user_id):
print(f"Successfully deleted user: {display_name} ({upn})")
deletion_count += 1
else:
print(f"Failed to delete user: {display_name} ({upn})")
print("-" * 30)
print(f"Operation completed. {deletion_count} out of {total_users} inactive users were deleted.")
copied