Highlighted
Community Manager
Community Manager

How to forcefully delete an entire folder ? vifs.pl does not support this feature

I'm trying to delete an entire directory and I was hoping to use RCLI vifs.pl command, but unfortunately even though there is a --force flag in conjunction with --rmdir, it does not allow you to delete a directory if it contain files. I also see that the vifs.pl command does not support listing of files without having to go to dsbrowser.pl (which I'm hoping I don't have to use).

What is the preferred method of removing an entire directory when passing a parameter of the following "\[datastore\] vm". I wanted to preserve as much of the original default commands within VMware VIMA as possibly without having to include a patch fix or side script

I was thinking of searching the specified directory and looking at the files and remoing it using vifs.pl ? Though I'm not getting any files being printed out and also how might I handle recursive deletes if the directory contains multiple directories with .vmdk under it?

my $eBrowser = Vim::get_view (mo_ref => $vm_view->environmentBrowser);
my $browser = Vim::get_view (mo_ref => $eBrowser->datastoreBrowser);
my $ds_path = "$TMP_DIR";
my $file_query = FileQueryFlags->new(fileSize => 0,fileType => 0,modification => 0);
my $searchSpec = HostDatastoreBrowserSearchSpec->new(details => $file_query,matchPattern => \["*.vmx", "*.vmdk"\]);
my $search_res = $browser->SearchDatastoreSubFolders(datastorePath => $ds_path,searchSpec => $searchSpec);

if ($search_res) {
        foreach my $result (@$search_res) {
                my $files = $result->file;
                        if ($files) {
                                foreach my $file (@$files) {
                                        #call vifs.pl to remove each file??
                                        print $file,"\n";
                                }
                        }
        }
}

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

Tags (2)
0 Kudos
9 Replies
Highlighted
Community Manager
Community Manager

Come to think about it, even the logic from the previous comment will not work because each time you call vifs.pl you'll be connecting to the host to just delete one file, this would be very inefficient and probably very slow.

I'm open to any other solution that might be available

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
Highlighted
Expert
Expert

Have you tried the destroy_Task() inherited from ManagedEntity?

See: http://pubs.vmware.com/vi-sdk/visdk250/ReferenceGuide/vim.ManagedEntity.html#destroy

Steve JIN, VMware Engineering

Creator of VMware Infrastructure Java API:

VI Java API 2.0 --- 15 times faster than AXIS in loading, 4+ faster in deserialization; only 1/4 of the size required by AXIS. More importantly freedom to redistribute your applications.

Download, Samples, DocWiki, RSS Feed

Steve JIN Author of VMware VI and vSphere SDK; Creator of open source VI Java API (http://vijava.sf.net); Blogger at http://www.doublecloud.org
0 Kudos
Highlighted
Community Manager
Community Manager

With this method, would I be able to pass in a folder that is on a datastore seen by the ESX/ESXi host?

(e.g.)

"\[MY_DATASTORE\] SOME_FOLDER"

which could contain .vmx and .vmdk along with sub-directories containing similar files?

If so, would I need a reference to datastore and generating some type of spec to pass to the destroy_Task()

Thanks for the quick feedback Steve

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
Highlighted
Expert
Expert

OK. I take back what I suggested. The destroy_Task() is for the folder in the invetory. Sorry.

To delete a generic folder in the datastore, you can try:

If you just want to delete the files with a VM, you can try the destroy_Task() on the VM object itself.

Steve JIN, VMware Engineering

Creator of VMware Infrastructure Java API:

VI Java API 2.0 --- 15 times faster than AXIS in loading, 4+ faster in deserialization; only 1/4 of the size required by AXIS. More importantly freedom to redistribute your applications.

Download, Samples, DocWiki, RSS Feed

Steve JIN Author of VMware VI and vSphere SDK; Creator of open source VI Java API (http://vijava.sf.net); Blogger at http://www.doublecloud.org
0 Kudos
Highlighted
Community Manager
Community Manager

Yes that is what vifs.pl is basically based on but I don't think it supports for recursive or a force delete of a directory that contains files. It gets a reference to fileManager in VIExt.PM

sub get_file_manager {
   my $service_content = Vim::get_service_content();
   my $fm = Vim::get_view (mo_ref => $service_content->{fileManager});
   return $fm;
}

and then tries to delete with:

sub do_rm {
   my ($fm, $remote_path_to_remove, $flag) = @_;
   my ($mode, $dc, $ds, $filepath) = VIExt::parse_remote_path($remote_path_to_remove);
   my $remote_path = "[$ds] $filepath";
   # bug 266928
   if((!$flag) && ($mode eq "folder" )){
        request_confirm("Remove '$filepath'? (y/n) :")
   }

   if ($mode eq "host") {
      print STDERR "Deleting of paths in /host is not supported.\n";
      return;
   } else {
      eval {
         $fm->DeleteDatastoreFile(name => $remote_path);
         print "Deleted file $remote_path successfully.";
      };
      if ($@) {
         VIExt::fail("Unable to delete " . $remote_path . ":\n" . ($@->fault_string));
      }
   }
}

Problem is I don't think this is supported

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
Highlighted
Community Manager
Community Manager

Looking at the API it's not supporting recursive deletes ... sounds like my only option is to ghetto rig it and register the VM just so I can call destroy_task() to properly delete the folder. I think this is something that should be added as a feature request to vifs, to provide a a remote file management as similar as possible to a standard console that many are use to.

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
Highlighted
Expert
Expert

Using "DeleteDatastoreFile" API, you can delete a directory containing files. It is the design of vifs.pl that restricts the remove option to delete a directory, only if it is empty.

You can use the DeleteDatastoreFile API as follows to delete a directory that does or does not contain the files.

my $path = Opts::get_option('path');

my $service_content = Vim::get_service_content();

my $fm = Vim::get_view (mo_ref => $service_content->);

eval {

$fm->DeleteDatastoreFile(name => $path);

print "Deleted files at $path successfully.";

};

0 Kudos
Highlighted
Community Manager
Community Manager

Steve,

I guess I spoke too soon, you're right the limitation is in vifs.pl. I think this is something that would be useful to have out of the box without having to make any adjustments. Thanks once again and I was hoping I did not have to ghetto rig the solution 😃

=========================================================================

--William

VMware ESX/ESXi scripts and resources at:

0 Kudos
Highlighted
Contributor
Contributor

use "vifs --force  <connection option> --rm  '[Datastore-XX] <dirname>'

0 Kudos