VMware Cloud Community
Mahiee
Enthusiast
Enthusiast

Deletion of unused VMDK (Zombie files)

Dear All - I found lot of Zombie files as per RVtools report, which I would like to review and delete them but as the list is huge it would take much time.

Can someone please help me with the script which must first check that LUN is truly unused and then go for deletion.

1. check and give the result that the LUN is unused.

2. Delete the LUN which is unused.

3. Return the LUN result which might be used

Thanks in Advance.

Rachis.

Tags (3)
18 Replies
LucD
Leadership
Leadership

Do you really mean LUN?
Or are you referring to ghosted VMDK files?


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

Reply
0 Kudos
Mahiee
Enthusiast
Enthusiast

I really mean to delete the orphaned vmdk files.

Thank you so much for your response.

Reply
0 Kudos
LucD
Leadership
Leadership

The script in Orphaned files and folders – Spring cleaning has a Delete switch.

But I strongly advise to review the list of returned orphaned VMDK files before using that switch!


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

Mahiee
Enthusiast
Enthusiast

Thank you very much for your script, well if we have a script or a command to check (review) whether that VMDK is used or unused that would be very helpful and also make my target done very quickly.

I request can you also please propose with script or command (or may be different script is also OK) just to "review the orphaned files" and return that is really unused.

Thanks a lot.

Rachis.

Reply
0 Kudos
LucD
Leadership
Leadership

Well, the script checks if a VMDK is attached to a registered VM.

If it is not, it will list the VMDK.

But, it could be that a VM is unregistered by mistake.
So I would still suggest a manual check of the produced list of VMDK files.
You have knowledge of the environment, that is hard/impossible to code in a script!

A look at the last update time of the VMDK could also give an indication.

No, afaik there are no commands or scripts that intelligently determine if a VMDK is still needed or not.

Like I said, the script will determine technically if a VMDK is orphaned or not, the final decision is still to be done by a human knowing the environment.


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

Mahiee
Enthusiast
Enthusiast

Thank you so much LucD,

Well I strongly agree with you.

May I request you to help in the below format so that it would be fulfill my target precisely.

Script 1 - Export all the Orphaned files from the vCenter inventory.

Script 2 - check and return the result that the script 1 results are not registered with any VM. Or can we have a check point like try to rename the files, if it is already in use it wont allow to rename.

Script 3 - Delete all the VMDK orphaned files which are validated in Script 2.

Much appreciated if you can help with that, it means a lot.

Even if you can give me one script where three functions all together consists is welcomed.

Thank you so much as always!

Reply
0 Kudos
LucD
Leadership
Leadership

Well, all three scripts are available in the script I pointed to earlier.

Script1 and script2 are the result when you run the script without the -Delete switch.

You can export (Export-Csv) the result to a CSV file for review and logging.

As a result of this you might want to register some VMs.

Now run the script again to check that you are only left with orphaned VMDK that can be deleted.

Then Script3 is done by adding the -Delete switch


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

Mahiee
Enthusiast
Enthusiast

Thank again for your prompt response,

Can you please validate the below script which would help my above two requirement or any amendments required (Script 1 + Script 2)

Also kindly confirm where the results will be stored with this script. (Any location has to be give!)

**************************************************************

function Remove-OrphanedData {

<#

.SYNOPSIS   Remove orphaned folders and VMDK files

.DESCRIPTION   The function searches orphaned folders and VMDK files

on one or more datastores and reports its findings.

Optionally the function removes  the orphaned folders   and VMDK files

.NOTES   Author:  Luc Dekens

.PARAMETER Datastore

One or more datastores.

The default is to investigate all shared VMFS datastores

.PARAMETER Delete

A switch that indicates if you want to remove the folders

and VMDK files

.EXAMPLE

PS> Remove-OrphanedData -Datastore ds1

.EXAMPLE

PS> Get-Datastore ds* | Remove-OrphanedData

.EXAMPLE

PS> Remove-OrphanedData -Datastore $ds -Delete

#>

[CmdletBinding(SupportsShouldProcess=$true)]

param(

[parameter(Mandatory=$true,ValueFromPipeline=$true)]

[PSObject[]]$Datastore

)

begin{

$fldList = @{}

$hdList = @{}

$fileMgr = Get-View FileManager

}

process{

foreach($ds in $Datastore){

if($ds.GetType().Name -eq "String"){

$ds = Get-Datastore -Name $ds

}

if($ds.Type -eq "VMFS" -and $ds.ExtensionData.Summary.MultipleHostAccess){

Get-VM -Datastore $ds | %{

$_.Extensiondata.LayoutEx.File | where{"diskDescriptor","diskExtent" -contains $_.Type} | %{

$fldList[$_.Name.Split('/')[0]] = $_.Name

$hdList[$_.Name] = $_.Name

}

}

Get-Template | where {$_.DatastoreIdList -contains $ds.Id} | %{

$_.Extensiondata.LayoutEx.File | where{"diskDescriptor","diskExtent" -contains $_.Type} | %{

$fldList[$_.Name.Split('/')[0]] = $_.Name

$hdList[$_.Name] = $_.Name

}

}

$dc = $ds.Datacenter.Extensiondata

$flags = New-Object VMware.Vim.FileQueryFlags

$flags.FileSize = $true

$flags.FileType = $true

$disk = New-Object VMware.Vim.VmDiskFileQuery

$disk.details = New-Object VMware.Vim.VmDiskFileQueryFlags

$disk.details.capacityKb = $true

$disk.details.diskExtents = $true

$disk.details.diskType = $true

$disk.details.thin = $true

$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

$searchSpec.details = $flags

$searchSpec.Query += $disk

$searchSpec.sortFoldersFirst = $true

$dsBrowser = Get-View $ds.ExtensionData.browser

$rootPath = "[" + $ds.Name + "]"

$searchResult = $dsBrowser.SearchDatastoreSubFolders($rootPath, $searchSpec)

foreach($folder in $searchResult){

if($fldList.ContainsKey($folder.FolderPath.TrimEnd('/'))){

foreach ($file in $folder.File){

if(!$hdList.ContainsKey($folder.FolderPath + $file.Path)){

New-Object PSObject -Property @{

Folder = $folder.FolderPath

Name = $file.Path

Size = $file.FileSize

CapacityKB = $file.CapacityKb

Thin = $file.Thin

Extents = [string]::Join(',',($file.DiskExtents))

}

}

}

}

}

}

}

}

}

}

Reply
0 Kudos
LucD
Leadership
Leadership

Not sure what exactly you want.

In the post, under the Sample Runs section, there are some examples on how the function can be used.

You pass one or more datastores on the Datastore parameter, the found orphaned VMDK are returned.

You can redirect this output to for example a CSV file.

Remove-OrphanedData -Datastore "ds1","ds2" | Export-Csv report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
Mahiee
Enthusiast
Enthusiast

Hi LucD,  sems above link is not working today! Can you please check and confirm if there is any issue.

Reply
0 Kudos
LucD
Leadership
Leadership

Yes, there is an issue with that site.

We are trying to fix it.


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

Reply
0 Kudos
Gungazoo1
Contributor
Contributor

LucD -- thanks for this and all of the code and help you put out there.

I'm having an issue with the script.  I think it has to do with the fact that my datastores have underscores in the names.  Does that seem plausible?  And, if so, is there an easy fix?  I'd love to use your function to find orphaned VMDK files.

Thanks again!

Peter

Reply
0 Kudos
LucD
Leadership
Leadership

Underscores in datastore names shouldn't make a difference.

Which issues are you seeing?

Are there error messages?


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

Reply
0 Kudos
Gungazoo1
Contributor
Contributor

I see errors like this:

 

PS /home/peter/src/windows/peterscripts> Get-Datastore nsxu-devu_01 |remove-orphaneddata
MethodInvocationException: /home/peter/src/windows/peterscripts/remove-orphaneddata.psm1:76
Line |
  76 |  …             $searchResult = $dsBrowser.SearchDatastoreSubFolders($roo …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "SearchDatastoreSubFolders" with "2" argument(s): "Invalid datastore path '[" + nsxu-devu_01.Name + "]'."

 

If I call it against the local datastore which doesn't have an underscore, but also doesn't have anything on it, it returns without error.

 

Thanks.

Reply
0 Kudos
LucD
Leadership
Leadership

I suspect the error might come from the line before the one with the error

$rootPath = "[" + $ds.Name + "]"

The error message shows [" + nsxu-devu_01.Name + "] which seems to suggest that value in $rootPath is incorrect.

Can you check that the double quotes are around both square brackets?


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

Reply
0 Kudos
Gungazoo1
Contributor
Contributor

You were right of course!  I was so focused on what I thought it was I missed the obvious.

Thanks so much and hope you have a great weekend!

Peter

bikashyadav
Contributor
Contributor

HI LucD,

 

How can we change the size and capacity to reflect n GB?

 

Thanks

Bikash

Reply
0 Kudos
LucD
Leadership
Leadership

Replace the CapacitKB line with

CapacityGB = [math]::Round($file.CapacityKb/1MB)


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

Reply
0 Kudos