How to delete any SPO site after certain time automatically by scheduling any task or something similar actions from admin side by checking its usability or usage like users are not using them actively and they should be notified saying your site would be

Vinod Survase 4,776 Reputation points
2023-12-14T09:45:31.5033333+00:00

How to delete any SPO site after certain time automatically by scheduling any task or something similar actions from admin side by checking its usability or usage like users are not using them actively and they should be notified saying your site would be deleted after so and so days or time?

Another question would be: Is there any script or any other way by using we can run and identify those stale SPO sites which are linked to teams and non teams and as well ?

Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft 365 and Office | OneDrive | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Xyza Xue_MSFT 30,176 Reputation points Microsoft External Staff
    2023-12-15T02:44:30.16+00:00

    Hi @Vinod Survase ,

    For your question:

    1.To delete any SharePoint Online (SPO) site after a certain time automatically, you can create a custom site policy that automatically deletes sites that have been inactive for a specified period of time.

    Once the site is close to the deletion date by means of the policy you set up above, you will constantly be reminded about that fact. First, if you log in to the site, you will see a notification message appearing on the top. Secondly, the site collection administrator will receive an email with a warning message.

    For more detail steps,please refer to this article:https://sharepointmaven.com/how-to-set-site-closure-and-deletion-policies-in-sharepoint-online/

    2.Identifying stale SPO sites that are associated with teams and non-teams can be done through the "Teams" and "Last activity (UTC)" parameters in the Sharepoint Admin center.

    User's image

    Or you can Get Unused Site Collections in the past 30 days using Pnp PowerShell:

    $TenantAdminURL = "https://Crescent-Admin.SharePoint.com"
    $TenantURL = "https://Crescent.SharePoint.com"
    $DaysInActive = 30 # number of days to check since last modified
    $CSVPath = "C:\Temp\InactiveSites.csv"
     
    #Connect to Admin Center
    Connect-PnPOnline -Url $TenantAdminURL -Credentials $Cred
         
    #Get All Site collections - Exclude BOT, Video Portals and MySites
    $SiteCollections = Get-PnPTenantSite -Filter "Url -like $TenantURL -and Url -notlike '-my.sharepoint.com/' -and Url -notlike '/portals/'"
     
    #Calculate the Date
    $Date = (Get-Date).AddDays(-$DaysInActive).ToString("MM/dd/yyyy")
     
    #Get All Site collections where the content modified
    $InActiveSites = $SiteCollections | Where {$_.LastContentModifiedDate -le $Date} | Select Title, Url, LastContentModifiedDate, Template, StorageMaximumLevel
     
    #Export to CSV
    $InActiveSites
    $InActiveSites | Export-Csv -Path $CSVPath -NoTypeInformation
    

    User's image

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.