VMware Cloud Community
jooshilaig
Enthusiast
Enthusiast
Jump to solution

Script for Deleting a Folder with its contents from a Datastore (vSAN or VMFS )

Hi Friends

Looking for a script to delete folders with its contents from a Datastore , I tried to do it with below Managed object  , but not able to find a method that can be used for deleting a folder

Appreciate if anyone can help with it

SearchDatastoreSubFolders_Task(searchSubFolders)

Thanks

Jooshil

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You need the FileManager for that.

Try like this, it will delete the folder and all its content.

$file = '[MyDS] TestFolder'

$dcName = 'MyDC'


$dc = Get-Datacenter -Name $dcName

$si = Get-View ServiceInstance

$fileMgr = Get-View -Id $si.Content.FileManager

$fileMgr.DeleteDatastoreFile($file, $dc.ExtensionData.MoRef)


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

You need the FileManager for that.

Try like this, it will delete the folder and all its content.

$file = '[MyDS] TestFolder'

$dcName = 'MyDC'


$dc = Get-Datacenter -Name $dcName

$si = Get-View ServiceInstance

$fileMgr = Get-View -Id $si.Content.FileManager

$fileMgr.DeleteDatastoreFile($file, $dc.ExtensionData.MoRef)


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

0 Kudos
jooshilaig
Enthusiast
Enthusiast
Jump to solution

Thanks Luc

That works Smiley Happy

0 Kudos
jlkumar07
Contributor
Contributor
Jump to solution

HI LUC,

                    is there any script to Delete the Empty folders in Data stores.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This will also delete an empty folder.


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

0 Kudos
jlkumar07
Contributor
Contributor
Jump to solution

Great, please need a script to delete the ONLY empty folders in Datastore. we don't want touch any other folders.

0 Kudos