emerson132
Enthusiast
Enthusiast

Check orphaned vmdk files are not being used before deletion

Jump to solution

Hi All,

I am working at a site and after looking through the vSphere platform, it has come to light that there are THOUSANDS (well over 10,000) of orphaned VMDK files, looks like there is a bug in the backup solution (you think).

Anyway what I was hoping someone could please help do you know if there is a way to confirm that a vmdk is definitely not required, the tools I have used to identify the orphaned vmdks are the script here  Virtually Jason: Orphaned VMDK Files and RV Tools (health tab).

What I need to do now is to automate the deletion of the vmdks if possible, but I need to run a sanity check first to ensure the files are genuinely orphaned.

Any help or tips greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
pkolom
Enthusiast
Enthusiast

Hi,

I can give you only few tips and rough concepts because I dont know the details.

First, this screams for PowerCLI, below you'll get some commands. If you're not familiar with PowerCLI/Powershell I highly recommend to get a grasp on it.

Second I'll assume that orphaned files = not used files. If that's not the case, you can always rename/move file instead of deleting and wait for those phonecalls/tickets Smiley Wink

Third, since we are going to work on files on datastores, we'll utilize "vmstores" PSDrive. Get-PSdrive cmdlet will give you all drives you have, one of them is vmstores Smiley Happy

C:\Windows\system32> Get-PSDrive

Name           Used (GB)     Free (GB) Provider      Root                                                                                                                   CurrentLocation

----           ---------     --------- --------      ----                                                                                                                   ---------------

Alias                                  Alias                                                                                                                                            

C                  52,39         44,97 FileSystem    C:\                                                                                                                   Windows\system32

Cert                                   Certificate   \                                                                                                                                  

D                 218,60        149,80 FileSystem    D:\                                                                                                                                

E                                      FileSystem    E:\                                                                                                                                

Env                                    Environment                                                                                                                                      

Function                               Function                                                                                                                                         

HKCU                                   Registry      HKEY_CURRENT_USER                                                                                                                  

HKLM                                   Registry      HKEY_LOCAL_MACHINE                                                                                                                 

Variable                               Variable                                                                                                                                         

vi                                     VimInventory  \LastConnectedVCenterServer                                                                                                        

vis                                    VimInventory  \                                                                                                                                  

vmstore                                VimDatastore  \LastConnectedVCenterServer                                                                                                        

vmstores                               VimDatastore  \                                                                                

WSMan                                  WSMan                         

To get to your datastores do typical "cd":

cd vmstores:\your_vcenter\your_cluster

On each level you can do "ls" or "cmd". Tab completion works on everything so use it Smiley Happy

From now all depends on what you already have and how it looks. Does those files have some naming pattern containing VM name or other information ("backup", "tmp" etc)? Do you have those names stored somewhere? In text file or csv?

I assume that you have file list in text file. So simplest method would be scanning through all datastores and all files. As you can imagine it is very time and resource consuming, so not recommended at all Smiley Happy

#we're importing filenames to variable

$oldfiles = Get-Content your_text_file.txt

cd vmstores:\your_vcenter\your_cluster

$oldfiles | ForEach-Object {

#we're recursivly looking for file stored in $_ special variable (current object)

$deleteme = Get-ChildItem $_ -Recurse

#deleting the file

Remove-Item $deleteme.PSPath

}

If filenames which you already have can somehow correlate directly to VM, then we could use smarter approach.

Let's say we have file testVM001-bak003.vmdk and VMname is testVM001. We can remove all chars after 9th char and then work on it:

#we're importing filenames to variable

$oldfiles = Get-Content your_text_file.txt

$oldfiles | ForEach-Object {

    #getting VM name

    $vmname = $_.Remove(9)

    #getting VM path

    $vmpath = (Get-VM -Name $vmname |Get-Datastore).DatastoreBrowserPath

    #combining full path

    $fullpath = $vmpath + "\" + $vmname

    cd $fullpath

    #deleting the file

    Remove-Item ./$_

}

Those are simple ideas, hope it helps.

View solution in original post

0 Kudos
4 Replies
vijayrana968
Virtuoso
Virtuoso

Check this if it works for you.

Find Orphaned VMDK's

0 Kudos
emerson132
Enthusiast
Enthusiast

Thanks for the reply, I have actually already got a script that identifies orphaned vmdks, what I need now is a process of removing them safely, I need to ensure that before I remove them they are not currently being used.

0 Kudos
pkolom
Enthusiast
Enthusiast

Hi,

I can give you only few tips and rough concepts because I dont know the details.

First, this screams for PowerCLI, below you'll get some commands. If you're not familiar with PowerCLI/Powershell I highly recommend to get a grasp on it.

Second I'll assume that orphaned files = not used files. If that's not the case, you can always rename/move file instead of deleting and wait for those phonecalls/tickets Smiley Wink

Third, since we are going to work on files on datastores, we'll utilize "vmstores" PSDrive. Get-PSdrive cmdlet will give you all drives you have, one of them is vmstores Smiley Happy

C:\Windows\system32> Get-PSDrive

Name           Used (GB)     Free (GB) Provider      Root                                                                                                                   CurrentLocation

----           ---------     --------- --------      ----                                                                                                                   ---------------

Alias                                  Alias                                                                                                                                            

C                  52,39         44,97 FileSystem    C:\                                                                                                                   Windows\system32

Cert                                   Certificate   \                                                                                                                                  

D                 218,60        149,80 FileSystem    D:\                                                                                                                                

E                                      FileSystem    E:\                                                                                                                                

Env                                    Environment                                                                                                                                      

Function                               Function                                                                                                                                         

HKCU                                   Registry      HKEY_CURRENT_USER                                                                                                                  

HKLM                                   Registry      HKEY_LOCAL_MACHINE                                                                                                                 

Variable                               Variable                                                                                                                                         

vi                                     VimInventory  \LastConnectedVCenterServer                                                                                                        

vis                                    VimInventory  \                                                                                                                                  

vmstore                                VimDatastore  \LastConnectedVCenterServer                                                                                                        

vmstores                               VimDatastore  \                                                                                

WSMan                                  WSMan                         

To get to your datastores do typical "cd":

cd vmstores:\your_vcenter\your_cluster

On each level you can do "ls" or "cmd". Tab completion works on everything so use it Smiley Happy

From now all depends on what you already have and how it looks. Does those files have some naming pattern containing VM name or other information ("backup", "tmp" etc)? Do you have those names stored somewhere? In text file or csv?

I assume that you have file list in text file. So simplest method would be scanning through all datastores and all files. As you can imagine it is very time and resource consuming, so not recommended at all Smiley Happy

#we're importing filenames to variable

$oldfiles = Get-Content your_text_file.txt

cd vmstores:\your_vcenter\your_cluster

$oldfiles | ForEach-Object {

#we're recursivly looking for file stored in $_ special variable (current object)

$deleteme = Get-ChildItem $_ -Recurse

#deleting the file

Remove-Item $deleteme.PSPath

}

If filenames which you already have can somehow correlate directly to VM, then we could use smarter approach.

Let's say we have file testVM001-bak003.vmdk and VMname is testVM001. We can remove all chars after 9th char and then work on it:

#we're importing filenames to variable

$oldfiles = Get-Content your_text_file.txt

$oldfiles | ForEach-Object {

    #getting VM name

    $vmname = $_.Remove(9)

    #getting VM path

    $vmpath = (Get-VM -Name $vmname |Get-Datastore).DatastoreBrowserPath

    #combining full path

    $fullpath = $vmpath + "\" + $vmname

    cd $fullpath

    #deleting the file

    Remove-Item ./$_

}

Those are simple ideas, hope it helps.

0 Kudos
emerson132
Enthusiast
Enthusiast

Thanks very much for your help I managed to get exactly what I wanted from your post.

0 Kudos