Content Type is still in use: Powershell to remove items and content types

This article describes a function of a custom SharePoint Online module available for download and installation here.

 

Issue description

You navigate to SharePoint List -> List Settings -> Scroll down to content types -> Open a content type

Click delete this content type and receive the following error message:

Most likely one of the items is still the content type. The following cmdlets will show how to find and remove such items.

 

Powershell

1. Download and install SharePoint Online SDK.

2. Download SPOMod available here.

3. Open SPOMod current file (in ISE or NotePad) and scroll down to the following lines:

 

# Paths to SDK. Please verify location on your computer.

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

4. Make sure that the paths correspond to the locations of SDK on your machine. Most often you need to change 16 into 15.

5. Save the file.

6. Open Powershell as an Administrator.

7. Run Import-Module PathToTheSPOModFile

8. Connect-SPOCSOM

9. Ready to go :)

Enter the following cmdlet. It will remove all items matching specific ContentTypeID:

 

foreach($item in(Get-SPOListItems -ListTitle YOURLISTTITLE -IncludeAllProperties $true | where {$_.ContentTypeId -match "0x0111002440A334027B18479FB4EDAFF1F149FF00AC40BD13F3F10A46A50C44E1F6D19EF0"})) { Remove-SPOListItem -ListTitle YOURLISTTITLE -ItemID $item.ID}

How to find content type ID in SharePoint Online?

 

 

The cmdlet involves data loss

The items will be removed from the Recycle Bin as well! There is no way to retrieve them.

 

Alternative UI approach

Not everyone likes Powershell. It is with sadness that I have to state that. Please find below a user-interface solution:

Step 1. Navigate to the list. Open the ribbon and click Modify view:

Step 2.Content type"

Step 3. Save the view.

Step 4. In the list click on the content type column and select the content type you are about to delete.

Step 5. You will see the applied filter.

Step 6. Mark all the items (they all should be showing the content type you want to delete).

Step 7. Delete the items.

 

References

Articles

Other articles on content types and Powershell:

Downloads

Scripts on content types available from Technet Gallery:

* Adding*

SharePoint Online: Create a content type using Powershell
Create content type and add directly to SPO list using Powershell
Create and add content type to a content type hub SharePoint Online
Create content type and add it to all lists in one site
Add Content Type to Task Lists
Add Content Type to Lists with Workflows
Add existing content type directly to SPO list using Powershell

*Getting *

Get all properties of all content types in a SharePoint site
Get All Properties of All Content Types (Detailed)
Get All Properties of All Content Types in All Lists (Detailed) across one site

Get properties of a single content type by its ID
Get content types belonging to a group
Get All Hidden Content Types added to the site
Get Single Content Type - Array Method
Get Names of all content types added to your SPO lists
Get Names of All Content Types
Get Names of all Available Content Types
Get Content Types Derived From One Parent
Get content types which cannot be modified
Get Content Types with a particular column
Find Custom Display Forms Deployed on your lists using Powershell
Verify the content types used in your lists against the default ones
Retrieve all Content Types from a Content Type Hub and their DisplayFormTemplate

Editing

"Unseal" sealed content types in SharePoint Online site collection
Modify the description of a list content type using Powershell
Modify the description of a site content type using Powershell
Unable to view items: modify the DisplayFormUrl back to default one
Modify the Display Form Template Name
Reset default content types
Add column (fieldlink) to a content type using Powershell
Assign your Content Types back to their default Groups using Powershell

 Content Type Management

Allow content type management for all lists in site collection using Powershell
Allow content type management for all lists in a site using Powershell
Set content type management setting for SharePoint Online list using Powershell
Custom Powershell cmdlet Set-SPOList -ContentTypesEnabled

SharePoint Online: Check content types added to your lists
SharePoint Online: Check content types added to your lists (recursive)
Get a report on lists where a given content type is added
SharePoint Online: Remove a content type from all lists in a site collection
Compare Web.AvailableContentTypes vs Web.ContentTypes