How to Delete SharePoint Pages Using PowerShell

How to Delete SharePoint Pages Using PowerShell

Managing SharePoint pages seems straightforward until you’re faced with old pages that no longer serve a purpose. Over time, these unused pages accumulate, creating navigation challenges and reducing site efficiency. For example, outdated pages can clutter search results, making it harder for users to locate relevant content. Addressing this issue not only improves user experience but also streamlines site management for administrators.

In this blog, we’ll explore how to efficiently delete SharePoint pages using both the SharePoint user interface & PowerShell.

Why You Might Need to Delete a SharePoint Page?

The most critical reason to delete a SharePoint page is to prevent users from accessing unnecessary or incorrect information.

  • In collaborative environments, content can change rapidly, and leaving old pages active can lead to confusion or miscommunication.
  • Unintentionally shared content can remain accessible, increasing the risk of exposing information to external audiences.
  • Deprecated templates left in the site may be reused accidentally, resulting in outdated or inconsistent content being published.
  • Pages containing sensitive information that is no longer relevant can still be accessed by unauthorized users, posing security and compliance risks.

By removing such pages, admins can reduce the risk of data breaches and ensure compliance with organizational security policies.

Different Ways to Delete a SharePoint Page

There are two main ways to delete a page from a SharePoint Online site, depending on your preference and the scale of the task. In the sections ahead, we’ll explore each method step by step.

  1. How to delete a page from a SharePoint site

a. Delete a page directly from the page itself

b. Delete a page from site pages library

2. How to delete a SharePoint page using PowerShell

a. Export all pages from all SharePoint sites

b. Delete pages across all SharePoint sites

c. Get all pages in a specific SharePoint site

d. Delete a specific SharePoint page using PowerShell

e. Bulk delete multiple pages based on conditions

f. Delete multiple pages by explicitly passing name

1. How to Delete a Page From a SharePoint Site

Deleting a page in SharePoint Online is hassle-free, making it the go-to method for quick cleanups. Since the option is built into the SharePoint interface, site owners and contributors can manage site pages efficiently.

Required Permission: Site owner or SharePoint administrator

a. Delete a Page Directly From the Page Itself

Deleting a page from within the page interface itself is a quick and user-friendly method in SharePoint Online. This method is ideal for users who are already in the page and want to remove it without navigating through the site pages library. Here’s how to do it step-by-step:

  1. Open the page you want to delete.
  2. Click Edit at the top right corner of the page to switch to edit mode.
  3. Now, select Page details.
  4. In the Page details pane, click the ‘Delete page’ under the More details section.
  5. A confirmation box will appear. Select Delete to confirm the deletion.

How-to-delete-page-in-SharePoint-Itself

Note: SharePoint retains deleted pages in the recycle bin for 93 days, after which they are permanently deleted. If needed, you can restore the page from the recycle bin, and its version history will remain intact.

b. Delete a Page From Site Pages Library

If you’re managing multiple pages or prefer a more structured view, deleting a page from the Site Pages library is a reliable method. Here’s how to do it:

  1. Navigate to the site where the page is located.
  2. On the top command bar of the site, select Site Contents.
  3. In the Site Contents page, click on Site Pages to view all pages stored in the site.
  4. Click the checkbox of a page you want to remove, then select the ellipses (…) next to the page name.
  5. From the dropdown menu, click Delete.
  6. Confirm the deletion by clicking the ‘Delete’ option.

How-to-delete-page-from-site-page-library

Note: Deleting a page from the site pages library removes it from site navigation, meaning those references will no longer work, but any direct links to the page (like embedded URLs) must be removed manually.

2. How to Delete a SharePoint Page Using PowerShell

For SharePoint admins, deleting pages through the UI can be tedious, especially when dealing with multiple pages across different sites. The interface doesn’t support bulk page deletion, so navigating through Site Pages libraries for each site can be time-consuming.

PowerShell solves this by offering a faster, scriptable way to manage page cleanup, whether you’re removing a single page or clearing out multiple pages.

Now, lets explore how to efficiently delete SharePoint pages using PowerShell under the following headings.

a. Export All Pages From All SharePoint Sites

Exporting all pages from SharePoint Online sites provides a comprehensive overview of your page landscape. It’s the first step to identify how many pages exist across your SharePoint sites, helping you decide which pages to delete.

First, make sure to register an Entra ID application to use with PnP PowerShell and then connect to the PnP PowerShell using the below cmdlet.

To export all pages across all SharePoint sites into a CSV file format, run the below script.

Replace –Path parameter with the location and filename where you want to save your CSV file.

Get-all-SharePoint-site-pages-using-PowerShell

The above script lists all pages across all sites in SharePoint Online. With the help of the created and last modified date in the output, you can easily spot pages that haven’t been updated in months.

b. Delete All Outdated Pages Across All SharePoint Sites

Removing outdated pages helps maintain a clean, organized environment and reduces clutter across multiple sites. To identify and remove these pages across your SharePoint sites, run the script below.

Replace –Path parameter with a path where the site page CSV file is located( as exported from the previous script). You can replace the method (Get-Date).AddMonths(-6) by the value of the created and modified timeframe. You can also modify the filter logic (or/and) to meet one or both conditions based on your requirements.

This script filters pages based on their creation and modification dates. Pages older than 6 months since creation and pages not modified for 3 months will be deleted. While running, a deletion prompt appears, you can confirm by entering ‘Y’ to continue. It displays the site URL along with the deleted page name, once you confirm the deletion.

c. Get All Pages in a Specific SharePoint Site

If you want to view all pages within a specific SharePoint site for auditing or cleanup planning, run the script below.

Replace –Url parameter with the specific site URL to retrieve the pages.

Get-all-pages-in-single-site

This script gives you details such as page name, creation date, and last modified date.

d. Delete a Specific SharePoint Page Using PowerShell

When you need to remove a particular modern page from your SharePoint site, the Remove-PnPPage parameter lets you do it quickly and precisely. To delete a specific page from the Site Pages library, run the following cmdlet and specify the page name.

Replace -Url ,Identity parameters with the site name and page name that you want to delete.

Delete-a-single-page-using-PowerShell

e. Bulk Delete Multiple Pages Based on Conditions

When managing a large SharePoint site, you may need to delete multiple pages that meet certain criteria, such as test pages, old drafts, or pages with a specific naming pattern. To bulk delete multiple pages with matching keywords, run the below script.

Replace Url with a site URL and replace like parameter with a condition (matching page name) that you want to delete.

Delete-multiple-pages-in-large-site

This script automates bulk deletion by removing only the pages that meet your specified conditions, saving time and reducing manual effort.

f. Delete Multiple Pages By Explicitly Passing Name

When you want to clean up several specific pages from a SharePoint site, this script lets you target them directly without manually navigating through the Site Pages library. To delete multiple pages, provide their names as comma-separated values inside the array as shown below and run the below script.

Replace Url with a site URL and replace $pageNames with actual page name you want to delete.

Delete-multiple-pages-by-explicitly-passing-names This script deletes only the pages you specify by name, ensuring a targeted and accurate cleanup process.

Best Practices for Page Lifecycle Management

  1. Leverage Draft and Publish States Wisely- A page begins as a local draft (private to the creator), moves to a shared draft (visible to editors), and is published to make it live. Use these states wisely to ensure content is reviewed before going public and to avoid accidental exposure.
  2. Use Page Templates for Consistency- Create and enforce standardized templates for different page types (e.g., news, announcements, documentation). This ensures visual and structural consistency across the site.
  3. Tag Pages with Metadata- Apply metadata like department, topic, or audience to improve searchability and filtering. This helps in organizing content and automating lifecycle actions.
  4. Employ Versioning to Safeguard and Restore- SharePoint maintains version history, allowing you to discard edits or restore previous versions, thereby giving flexibility and recovery options.
  5. Understand Page Locking and Co-Authoring Behaviour- When editing, pages are locked to prevent concurrent conflicts. However, in co-authoring-enabled environments, edits may auto-save, and locking is handled differently. Be aware of this to avoid data loss.

Therefore, efficient SharePoint page management is essential for keeping your workspace clean, organized, and easy to navigate. By utilizing these PowerShell scripts, you can automate cleanups, remove outdated contents, and ensure only relevant, secure pages remain.

I hope this blog helps you streamline page deletions and maintain SharePoint Online at scale with the control and precision PowerShell provides. If you have any questions or run into a challenge, drop it in the comments, we’re here to help.

How to Delete SharePoint Pages Using PowerShell

by Kanaga time to read: 7 min
0