2

I am able to delete the attachments but not the folder associated to that attachment. Each item of the SharePoint list has an attachment attached to it. Each item's attachment is stored in that particular item's folder. The name of the folder is the ID of the folder. Can you please help me in deleting the folder attached to that item also.

This is code that I've written to delete the attachment. Help me with the code to delete the folder attached to the attachment.

if ($item["rjhg"] -eq "MT (A400M)")
{
    $totalitemcount += $item["rjhg"].count;   
    for($i=$item.Attachments.count-1; $i -ge 0; $i--) 
    {
        #Write-Host "Request ID :" $item["ID"]
        [Microsoft.SharePoint.SPQuery]$query = New-Object Microsoft.SharePoint.SPQuery
        $query.ViewAttributes = "Scope='Recursive'"

        Write-Host "Attatchment : "+$item.Attachments[$i]
        $item.Attachments.Delete($item.Attachments[$i])

        Write-Host "File Deleted: $item.Attachments[$i]"
        $item.SystemUpdate()

        #$allitems=$item.GetItems($query)
        $filecount += $item.count
    }
}

1 Answer 1

2

We can get the Attachments folder using the PowerShell below.

$attachmentCollection = $item.Attachments
$folder = $web.GetFolder($attachmentCollection.UrlPrefix);

Unfortunately, we can't use the code below to delete this folder, it is a SharePoint default folder, we can't delete it. You will get the exception (Exception calling "Delete" with "0" arguments(s): "The method is not allowed on this folder.").

$folder.Delete()

If you don't want to add attachments for the list, we can go to List Settings-> Advanced Settings-> set "Attachments to list items are:" to Disabled.

enter image description here

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.