VMware Cloud Community
jlkumar07
Contributor
Contributor
Jump to solution

PowerCli script to Remove Empty Folders in VMFS(vSAN,VMFS) DataStores

please provide the powercli script to remove Empty Folders in vmfs(vSAN,VMFS) Datastores

0 Kudos
1 Solution

Accepted Solutions
6 Replies
LucD
Leadership
Leadership
Jump to solution

0 Kudos
jlkumar07
Contributor
Contributor
Jump to solution

we have issue with vsphere replication. we are vsphere replication and when cleanup the replication , it is deleting files inside folder and leaving empty folders. we need to cleanup those empty folders in datastores. does the above script will do that?. we need to cleanup only empty folders in DS.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, it deletes a folder, empty or not.


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

0 Kudos
jlkumar07
Contributor
Contributor
Jump to solution

Great, sorry for confusion  we need to delete only empty folders from DS and should not touch any other folders with the content in them.

we are performing vsphere replication when we unconfigure replication it is deleting the every file in folder and leaving the folder in DS. so please check once and give me script to delet ONLY empty folders from DS

0 Kudos
LucD
Leadership
Leadership
Jump to solution

So you also want code to find empty folders?


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you check if this actually finds all your empty folders on a datastore?

$dsName = 'MyDatastore'

$ds = Get-Datastore -Name $dsName

$dsBrowser = Get-View -Id $ds.ExtensionData.Browser


$spec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec


$details = New-Object VMware.Vim.FileQueryFlags

$details.FileOwner = $true

$details.FileSize = $true

$details.FileType = $true

$details.Modification = $true

$spec.Details = $details


$query = New-Object VMware.Vim.FolderFileQuery

$spec.Query += $query


$path = "[$($ds.Name)]"

$dsBrowser.SearchDatastoreSubFolders($path, $spec) |

   ForEach-Object -Process {

   $folder = $_.FolderPath

   if ($folder -match "]$") {

   $folder += ' '

   }

   $_.File | where {$_.Path -notmatch '^\.' -and $_.FileSize -eq 280} |

   ForEach-Object -Process {

   Write-Host "Empty folder: $($folder)$($_.Path)" 

   }

}


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

0 Kudos