VMware Employee
VMware Employee

Delete old files - Datastore Spring Clean

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

vSphere 6.5 U2


0 Kudos
1 Reply

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.

Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos