Automation

 View Only
  • 1.  Delete old files - Datastore Spring Clean

    Broadcom Employee
    Posted Sep 17, 2018 10:12 AM

    Is there a script available that moves/delete all files on a datastore older than a specified date?

    vSphere 6.5 U2

    LucD



  • 2.  RE: Delete old files - Datastore Spring Clean

    Posted Sep 17, 2018 10:33 AM

    You can use the DatastoreProvider to discover all the files.

    Note that the files only have a property that states when the last write to the file happened.

    You can use this as a criteria for the selection of the files.

    But be aware that it always dangerous to go deleting files just like that.

    You have to be absolutely sure that the file is not in use anymore.

    To find the candidate files, you could do

    $dsName = 'MyDS'

    $targetDate = (Get-Date).AddDays(-7)

    $ds = Get-Datastore -Name $dsName

    New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null

    Get-ChildItem -Path DS:\ -Recurse |

    where{$_.LastWriteTime -lt $targetDate} |

    Select LastWriteTime,ItemType,DatastoreFullPath

    Remove-PSDrive -Name DS -Confirm:$false

    If this indeed discovers the files you are looking for, it is easy to remove them from this same script.

    Copying the files to another folder would be a bit more challenging.