Delete User Account
User Management
Delete User Account
Permanently delete a user account and all associated data
DELETE
Delete User Account
Overview
This endpoint permanently deletes a user account from the system. This action is irreversible and will cascade to delete all associated data including generated passwords linked to the user.Authentication
This endpoint requires authentication. Include a valid JWT access token in the Authorization header:Authorization
Authentication is required but the current implementation does not explicitly verify that the authenticated user matches theuser_id being deleted. This means proper authorization checks should be implemented at the application level or added to the view logic.
Request
Endpoint
Path Parameters
Headers
Request Body
No request body required.Response
Success Response (204 No Content)
Returns a success message when the user is deleted:Error Responses
401 Unauthorized
Returned when the authentication token is missing or invalid:404 Not Found
Returned when the user ID doesn’t exist:500 Internal Server Error
Returned when an unexpected error occurs during deletion:Example Request
Example Response
Implementation Details
This endpoint is implemented in/apps/users/views.py:123 as the delete_user function view:
- Decorated with
@permission_classes([IsAuthenticated])to require authentication - Uses
get_object_or_404to retrieve the user by ID (returns 404 if not found) - Calls
user.delete()which triggers Django’s cascading deletion - Returns a 204 No Content status with a success message
Cascading Deletion
When a user is deleted, Django’s ORM will automatically handle cascading deletions for related objects based on the foreign key relationships defined in the models. This typically includes:- Generated Passwords: All passwords created by the user will be deleted
- User Sessions: Active authentication sessions will be invalidated
- Media Files: Note that avatar images in the filesystem may need manual cleanup
Security Considerations
-
Authorization: The current implementation requires authentication but does not verify that the authenticated user matches the
user_idbeing deleted. Consider adding this check: - Soft Delete: Consider implementing soft deletion (marking users as inactive) instead of permanent deletion to maintain data integrity and audit trails.
- Confirmation: In a production environment, consider requiring additional confirmation (like password verification) before allowing account deletion.
- Audit Logging: Log user deletion events for security and compliance purposes.
Best Practices
- User Confirmation: Always require explicit user confirmation in the UI before calling this endpoint
- Data Export: Offer users the ability to export their data before deletion
- Grace Period: Consider implementing a grace period where accounts are deactivated first and permanently deleted after a waiting period
- Notification: Send email notifications to users confirming the account deletion
Important Warnings
Related Endpoints
- Get Profile - Retrieve user profile information
- Update Profile - Update user profile information
- Sign Up - Create a new user account
