How to Automatically Delete Disabled Microsoft 365 Users
Question details
The user wants to automate the permanent deletion of Microsoft 365 user accounts that have been disabled for a specified retention period.

- Product
- Microsoft 365
- Device & OS
- not provided
- Scenario
- Managing the Microsoft 365 user lifecycle and performing administrative clean-up for inactive accounts.
- Observed behavior
- The user needs a time-based automated deletion mechanism, which is not natively available as a toggle in the standard Microsoft 365 admin center interface.
Ensure you have global administrator or user administrator permissions in your Microsoft 365 tenant. It is highly recommended to secure legal holds or back up critical mailbox and OneDrive data before executing permanent deletion scripts.
Use a PowerShell Automation Script
Since Microsoft 365 does not have a native UI setting for time-based deletion, PowerShell is the standard and approved method for automating this user account lifecycle task.
The Microsoft 365 Admin Center lacks a built-in time-based deletion tool for disabled users. To achieve this, IT administrators must rely on Microsoft Graph PowerShell to script the deletion based on the account's disabled status and last activity date.
Always test your scripts in a non-production environment or utilize the '-WhatIf' parameter to preview the results before enabling permanent deletion.
Open Windows PowerShell as an Administrator and connect to your Microsoft 365 environment using the Connect-MgGraph command, ensuring you grant the required Directory.ReadWrite.All scopes.
Run a query to filter users whose AccountEnabled property is set to False, and check their last modification or sign-in date to ensure they meet your defined retention period.
Pipe the filtered user objects to the Remove-MgUser cmdlet to delete the accounts. You can use Windows Task Scheduler or Azure Automation to run this script automatically on a periodic schedule.

Need a Lightweight Office Suite for Your Team? Try WPS Office
While managing Microsoft 365 users requires complex PowerShell scripting, day-to-day document creation shouldn't be difficult. WPS Office provides a free, lightweight, and highly compatible alternative to Microsoft Office, perfect for teams looking to reduce licensing costs without sacrificing productivity.

Frequently Asked Questions
Can I auto-delete users directly from the Microsoft 365 Admin Center?
No, the standard Microsoft 365 Admin Center interface does not currently offer a built-in feature or setting to automatically delete disabled user accounts after a set time period.
What happens to user data when a Microsoft 365 account is deleted?
When a Microsoft 365 user account is deleted, their associated data (such as their Exchange mailbox and OneDrive files) is retained for a standard 30-day grace period. After 30 days, the data is permanently destroyed unless a specific retention hold was previously applied.
How can I prevent accidental deletion when automating with PowerShell?
Before running any automated PowerShell deletion script in production, implement detailed logging, use the '-WhatIf' parameter to safely preview which accounts will be affected, and configure safeguard checks to skip accounts with specific admin roles.
Where can I find examples of PowerShell scripts for Microsoft 365 automation?
You can find verified PowerShell script examples, module documentation, and community guidance in the official Microsoft Learn portal and Microsoft Q&A forums.




