none
want to delete the attachments only from the items 2 months ago RRS feed

  • Question

  • we have one list where it has 17000 items, and from which we want to delete the attachments only  from the items which should be 2 months ago items. can you please help on this. thanks
    Tuesday, June 18, 2019 9:42 AM

Answers

  • Hi SravanC, 

    I have edited my reply, i made a mistake, missing a "{" after if condition. 

    Using the following PowerShell:

    $web=Get-SPWeb -identity "http://SiteURL"
    $list=$web.lists["ListName"]
    $dateFilter=(Get-Date).AddMonths(-2) #Past Month
    foreach($item in $List.Items)
    {
    if($item["Attachments"] -eq "True" )
    {
    if($item["Created"]-lt $dateFilter)
    {
    for($i=$item.Attachments.count-1; $i -ge 0; $i--)
    {
    $item.Attachments.Delete($item.Attachments[$i])
    }
    }
    }
    $item.update()
    }

    Best Regards, 

    Lisa Chen 


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.



    Thursday, June 27, 2019 6:03 AM
    Moderator

All replies

  • Hi SravanC, 

    You can use PowerShell to delete the attachments only from the items 2 months ago.

    Delete all attachments  from SharePoint list using PowerShell for your reference:

    https://www.enjoysharepoint.com/delete-all-attachments-from-sharepoint-2013-list/

    PowerShell to delete items from a list where created date < "1/1/2011" for your reference:

    http://sharepoint.stackexchange.com/questions/16024/powershell-to-delete-items-from-a-list-where-created-date-1-1-2011

    http://sharepoint.stackexchange.com/questions/115500/powershell-to-delete-all-items-from-a-list-based-on-created-date

    Best Regards, 

    Lisa Chen 



    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, June 19, 2019 2:14 AM
    Moderator
  • Hi Lisa,

    Thank you

    Below is the command to delete all the attachments, but I am looking for  to delete the all attachments 2 months ago. can you share that script?

    $web=Get-SPWeb -identity “http://SiteURL”
    $list=$web.lists[“ListName”];
    foreach($item in $List.Items)
    {
    if( $Item[“Attachments”] -eq “True” )
    {
    for($i=$item.Attachments.count-1; $i -ge 0; $i–)
    {
    $Item.Attachments.Delete($item.Attachments[$i])
    }
    }
    $item.update()
    }

    Hi Lisa, One more point to add, instead of deletion,  can we archive all the attachments to other list ( all these attachments are 2 months ago) 


    • Edited by SravanC Thursday, June 20, 2019 5:44 AM
    Wednesday, June 19, 2019 6:55 AM
  • Hi SravanC, 

    Add a condition like the following:

    $dateFilter=(Get-Date).AddMonths(-2) #Past Month

    If($item["Created"]-lt $dateFilter)

    You can try the following PowerShell:

    $web=Get-SPWeb -identity "http://SiteURL"
    $list=$web.lists["ListName"]
    $dateFilter=(Get-Date).AddMonths(-2) #Past Month
    foreach($item in $List.Items)
    {
    if($item["Attachments"] -eq "True" )
    {
    if($item["Created"]-lt $dateFilter)
    {
    for($i=$item.Attachments.count-1; $i -ge 0; $i--)
    {
    $item.Attachments.Delete($item.Attachments[$i])
    }
    }
    }
    $item.update()
    }

    Here is a demo for your reference:

    http://www.sharepointdiary.com/2012/09/find-all-documents-created-modified-by-specific-users-datetime.html

    Best Regards, 

    Lisa Chen 


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.





    Tuesday, June 25, 2019 8:43 AM
    Moderator
  • I put a comment in the other thread opened on this. Lisa's answer is almost correct; She's filtering on the item created date and not the attachments.. Just an FYI

    If this is helpful please mark it so. Also if this solved your problem mark as answer.

    Tuesday, June 25, 2019 10:10 PM
    Moderator
  • Hi SravanC, 

    If the reply is helpful to you, you could mark the reply as answer. Thanks for your understanding.

    Best Regards, 

    Lisa Chen 


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, June 26, 2019 6:20 AM
    Moderator
  • Hi SravanC, 

    I have test the code in my environment and it works fine. 

    If the reply is helpful to you, you could mark the reply as answer. Thanks for your understanding.

    Best Regards, 

    Lisa Chen 


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


    Thursday, June 27, 2019 3:08 AM
    Moderator
  • HI Lisa,

    I have seen it now. Will check it. and same time is it possible to move the attachments to different library instead of deleting it? 

    • Edited by SravanC Thursday, June 27, 2019 5:13 AM
    Thursday, June 27, 2019 5:13 AM
  • Hi SravanC, 

    I understand that want to move the attachments to different library, however it would be better if you open up a new thread for the new question. And others will focus on your new issue and give their suggestions. 

    This will make answer searching in the forum easier and be beneficial to other community members as well.

    Thank you for your understanding.

    Best Regards, 

    Lisa Chen 



    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, June 27, 2019 5:16 AM
    Moderator
  • Fine
    Thursday, June 27, 2019 5:18 AM
  • Hi SravanC, 

    Copy Attachment from SharePoint List to Document Library using SharePoint Designer - PowerShell for your reference:

    http://www.sharepointdiary.com/2014/02/copy-attachment-from-list-to-document-library.html

    Best Regards, 

    Lisa Chen 


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, June 27, 2019 5:20 AM
    Moderator
  • Hi SravanC, 

    If the reply is helpful to you, you could mark the reply as answer. Thanks for your understanding.

    Best Regards, 

    Lisa Chen 


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, June 27, 2019 5:22 AM
    Moderator
  • Hi Lisa,

    I tried , getting below message

    and below is the script, please let me know where could be the wrong


    • Edited by SravanC Thursday, June 27, 2019 5:57 AM
    Thursday, June 27, 2019 5:57 AM
  • Hi SravanC, 

    I have edited my reply, i made a mistake, missing a "{" after if condition. 

    Using the following PowerShell:

    $web=Get-SPWeb -identity "http://SiteURL"
    $list=$web.lists["ListName"]
    $dateFilter=(Get-Date).AddMonths(-2) #Past Month
    foreach($item in $List.Items)
    {
    if($item["Attachments"] -eq "True" )
    {
    if($item["Created"]-lt $dateFilter)
    {
    for($i=$item.Attachments.count-1; $i -ge 0; $i--)
    {
    $item.Attachments.Delete($item.Attachments[$i])
    }
    }
    }
    $item.update()
    }

    Best Regards, 

    Lisa Chen 


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.



    Thursday, June 27, 2019 6:03 AM
    Moderator
  • Hi SravanC, 

    Is there anything update? 

    Best Regards, 

    Lisa Chen 


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, June 27, 2019 6:47 AM
    Moderator
  • Hi Lisa,

     I did it, it looks like it worked but why still it is showing 2017 attachment 

    Thursday, June 27, 2019 7:15 AM
  • Hi SravanC, 

    Does the "发现日期" mean "Created" date?

    I use the condition "if($item["Created"]-lt $dateFilter)" to judge the items which created two month ago. 

    Best Regards, 

    Lisa Chen 


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, June 27, 2019 7:23 AM
    Moderator
  • Hi Lisa,

    it is "Date of Discovery" 

    and other script ( below one ) worked but as expected it ran on all the attachments moving to library 

    Thursday, June 27, 2019 7:35 AM

  • Hi SravanC, 

    If you want to delete the attachments based on "Date of Discovery" column. 

    Change "if($item["Created"]-lt $dateFilter)" to "if($item["Date of Discovery"]-lt $dateFilter)".

    Best Regards, 

    Lisa Chen 



    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, June 27, 2019 7:43 AM
    Moderator
  • Thank you. It worked 

    but can you help me on script to copy the attachment from list to library ( 2months ago) 

    Thursday, June 27, 2019 7:58 AM
  • Hi SravanC, 

    Try the following code:

    $WebURL = "http://sharepoint.crescent.com/pmo/GIS/" $SourceListName = "External Projects" $TargetLibraryName = "Design Documents"

    $dateFilter=(Get-Date).AddMonths(-2) #Past Month

    #Get the Web List and Library objects $web = Get-SPWeb $WebURL $SourceList = $web.Lists[$SourceListName] $TargetLibrary = $web.Lists[$TargetLibraryName] #Loop through each list item foreach ($ListItem in $SourceList.Items) {

    if($item["Date of Discovery"]-lt $dateFilter)

    {

    if($ListItem.Attachments.Count -gt 0) { #Loop through each attachment in the list item foreach ($Attachment in $ListItem.Attachments) { #Get the attachment $file = $web.GetFile($ListItem.Attachments.UrlPrefix+$Attachment) $bytes = $file.OpenBinary() $TargetFileName = $TargetLibrary.RootFolder.Url+"/"+$Attachment $TargetFile = $TargetLibrary.RootFolder.Files.Add($TargetFileName, $bytes, $true) Write-Host "Copied to: $($TargetFilename)" } }

    } }

    Best Regards, 

    Lisa Chen 


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Thursday, June 27, 2019 8:15 AM
    Moderator
  • Hi Lisa,

    one clarification needed, earlier script which worked it is copying the attachments right? or is it moving the attachments?

    because i see there are items in the source list, so in case if we want to move? (source list should not contain attachments) 


    • Edited by SravanC Thursday, June 27, 2019 8:59 AM
    Thursday, June 27, 2019 8:39 AM