diff --git a/pkg/multicloud/esxi/storage.go b/pkg/multicloud/esxi/storage.go index 76e34f590e..4e451eb6ce 100644 --- a/pkg/multicloud/esxi/storage.go +++ b/pkg/multicloud/esxi/storage.go @@ -610,6 +610,28 @@ func (self *SDatastore) FilePutContent(ctx context.Context, remotePath string, c return self.Upload(ctx, remotePath, strings.NewReader(content)) } +// Delete2 can delete file from this Datastore. +// isNamespace: remotePath is uuid of namespace on vsan datastore +// force: ignore nonexistent files and arguments +func (self *SDatastore) Delete2(ctx context.Context, remotePath string, isNamespace, force bool) error { + var err error + ds := self.getDatastoreObj() + dc := self.datacenter.getObjectDatacenter() + if isNamespace { + nm := object.NewDatastoreNamespaceManager(self.manager.client.Client) + err = nm.DeleteDirectory(ctx, dc, remotePath) + } else { + fm := ds.NewFileManager(dc, force) + err = fm.Delete(ctx, remotePath) + } + + if err != nil && types.IsFileNotFound(err) && force { + // Ignore error + return nil + } + return err +} + func (self *SDatastore) Delete(ctx context.Context, remotePath string) error { url := self.GetPathUrl(remotePath) diff --git a/pkg/multicloud/esxi/vdisk.go b/pkg/multicloud/esxi/vdisk.go index 74965e9574..a753b4fa01 100644 --- a/pkg/multicloud/esxi/vdisk.go +++ b/pkg/multicloud/esxi/vdisk.go @@ -296,7 +296,7 @@ func (disk *SVirtualDisk) Delete(ctx context.Context) error { return err } ds := istorage.(*SDatastore) - return ds.DeleteVmdk(ctx, disk.getBackingInfo().GetFileName()) + return ds.Delete2(ctx, disk.getBackingInfo().GetFileName(), false, false) } func (disk *SVirtualDisk) CreateISnapshot(ctx context.Context, name string, desc string) (cloudprovider.ICloudSnapshot, error) { diff --git a/pkg/multicloud/esxi/virtualmachine.go b/pkg/multicloud/esxi/virtualmachine.go index 9adaa95037..ce6b265376 100644 --- a/pkg/multicloud/esxi/virtualmachine.go +++ b/pkg/multicloud/esxi/virtualmachine.go @@ -547,10 +547,6 @@ func (self *SVirtualMachine) doDetachDisk(ctx context.Context, vdisk *SVirtualDi removeSpec.Operation = types.VirtualDeviceConfigSpecOperationRemove removeSpec.Device = vdisk.dev - if remove { - removeSpec.FileOperation = types.VirtualDeviceConfigSpecFileOperationDestroy - } - spec := types.VirtualMachineConfigSpec{} spec.DeviceChange = []types.BaseVirtualDeviceConfigSpec{&removeSpec} @@ -568,7 +564,10 @@ func (self *SVirtualMachine) doDetachDisk(ctx context.Context, vdisk *SVirtualDi return err } - return nil + if !remove { + return nil + } + return vdisk.Delete(ctx) } func (self *SVirtualMachine) GetVNCInfo() (jsonutils.JSONObject, error) {