I need to remove empty <p> tags from all rich text fields in the content tree:


I've done nothing myself with Sitecore Powershell Extensions so far, so I am not sure if this is possible.

I've created a saveitempipeline so that this can't happen anymore, but we have a lot of fields with empty <p> tags so I'd like to remove them with SPE.

2 Answers 2


You can use the below script to remove all empty p/P tags (having space or &nbsp;).

This script uses the HtmlDecode method of System.Web.HttpUtility class to decode single or multiple occurrences of &nbsp; to an empty string and then Regex replaces p tags with or without spaces.

$items = Get-ChildItem "master:/sitecore/content" -Recurse -Language *

foreach($item in $items){
    Write-Host "Item  - " $item.Paths.Path
    foreach($field in $item.Fields){

        if($field.Type -eq "Rich Text"){

            $rawValue = $field.Value

            if($rawValue -like '*<p*'){
                $output = [System.Web.HttpUtility]::HtmlDecode($rawValue)
                $output =  $output -replace "(<(P|p)>)\s*(<\/(P|p)>)",'' #To remove <p></p> or <p> {HAVING SPACES IN BETWEEN} </p> or <P></P> or <P>  {HAVING SPACES IN BETWEEN}  </P>
                $output =  $output -replace "(<(P|p)\s*\/>)",''          #To remove self closed p tags e.g.  <p/> or <p         />

                $item.Fields[$field.Name].Value = $output

                Write-Host "Field - " $field.Name 
                Write-Host ""

Tested with

<p>p tag with text</p><p/><p /><p></p><div>hello</div><p>&nbsp;&nbsp;&nbsp;</p>


<p>p tag with text</p><div>hello</div>

Here is another take at solving this problem.

  • Use the HtmlAgilityPack (included with Sitecore) to parse the document
  • Check for each required scenario with a function
  • Save some time by only looking at known fields

Test Html

<div>Here is something</div>
<p>&nbsp; </p>
<p> &nbsp;</p>
<p>  &nbsp;&nbsp;</p>
<div>Something here<p>  &nbsp;&nbsp;</p></div>
<p>Here is something interesting</p>
<p><span>Some text</span></p>

Sample Script

function Remove-EmptyPTag {
    $htmlDocument = New-Object -TypeName HtmlAgilityPack.HtmlDocument
    $pNodes = $htmlDocument.DocumentNode.SelectNodes("//p")
    foreach($x in $pNodes) {
        # If the node is completely empty then we should remove
        if([string]::IsNullOrEmpty($x.InnerText)) {
        # If the node is only made up of spaces then we should remove
        if($x.InnerText -match "^(&nbsp;|\s)*$") {
    # Found that once the p node is removed you ended up with some empty nodes in the final document.
    $count = $htmlDocument.DocumentNode.ChildNodes.Count
    $nodes = [HtmlAgilityPack.HtmlNode[]]::new($count)
    $htmlDocument.DocumentNode.ChildNodes.CopyTo($nodes, 0)
    foreach($node in $nodes) { 
        if([string]::IsNullOrWhiteSpace($node.InnerText)) {


Get-Item -Path "master:" -ID "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}" |
    ForEach-Object { $_.Content = Remove-EmptyPTag -Text $_.Content }
  • Nice one! but I think regex needs a small fix to handle another empty p scenarios where there is a space before &nbsp; or space between multiple &nbsp; Commented Sep 7, 2019 at 21:27
  • Thanks Surya. Realized I needed the '|' pipe character to allow for any position. Commented Sep 8, 2019 at 1:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.