Orphaned or stale virtual machine removal from vCenter Server
search cancel

Orphaned or stale virtual machine removal from vCenter Server

book

Article ID: 311105

calendar_today

Updated On:

Products

VMware vCenter Server

Issue/Introduction

This article provides instructions for removing orphaned virtual machine or template records from the vCenter Server database. This is used when the objects cannot be removed via the vSphere Client.

Symptoms:

  • A virtual machine appears as "orphaned" or "inaccessible" in the vSphere Client.
  • The "Remove from Inventory" or "Delete from Disk" option is grayed out and cannot be selected.
  • The virtual machine is not listed when running vim-cmd vmsvc/getallvms on the ESXi host CLI.
  • Attempts to delete a VM template result in the error: The object 'vim.VirtualMachine:vm-####' has already been deleted or has not been completely created.
  • The "Remove from Inventory" or "Delete from Disk" option is grayed out and cannot be selected.

 

 

Environment

  • VMware vCenter Server 7.x / 8.x / 9.x
  • VMware vSphere ESXi 7.x / 8.x / 9.x

Cause

Metadata inconsistency occurs when a virtual machine or datastore is removed directly from an ESXi host or storage layer while vCenter Server is unaware of the change, leaving residual entries in the vCenter Server database (VCDB).

Resolution

Note: For method 2, create an offline snapshot of the vCenter Server Appliance (VCSA) before proceeding.  If the vCenter Server is part of a Linked Mode replication group, backups must be created for every member as described in KB Snapshot Best practices for vCenter Server Virtual Machines


Method 1: Dummy Folder Workaround

  1. In vSphere Client, change into the "VMs and Folders" view.
  2. Create a new Virtual Machine folder.
  3. Drag and drop the orphaned virtual machine into this folder.
  4. Delete the folder. 

Method 2: Manual Database Removal (Single VM Object) 

  1. SSH to the vCenter Server as root.
  2. Stop the vpxd service:
    bash
     
    service-control --stop vmware-vpxd
  3. Connect to the database:
    bash
     
    /opt/vmware/vpostgres/current/bin/psql -d VCDB -U postgres
  4. Identify the VM ID by searching for the VM name ((replace <vm name> against the actual name of the orphaned VM)):
    sql
     
    select id, name from vpx_entity where name like '%<vm name>%';
    1.   Example Output:

           VCDB=# select * from vpx_entity where name like '%vcsa%';
            id      |   name                | type_id  | parent_id
           --------+---------------------+------------+------------
            0000 | vcsa(orphaned) |    #        |    #

  5. Execute the following delete statements in order, replacing #### with the numeric ID found:
    sql
     
         delete from VPX_COMPUTE_RESOURCE_DAS_VM where VM_ID=<VM_ID>;
    
         delete from VPX_COMPUTE_RESOURCE_DRS_VM where VM_ID=<VM_ID>;
    
         delete from VPX_COMPUTE_RESOURCE_ORC_VM where VM_ID=<VM_ID>;
    
         delete from VPX_VM_SGXINFO where VM_ID=<VM_ID>;
    
         delete from VPX_GUEST_DISK where VM_ID=<VM_ID>;
    
         delete from VPX_VM_VIRTUAL_DEVICE where ID=<VM_ID>;
    
         delete from VPX_VM_DS_SPACE where VM_ID=<VM_ID>;
    
         delete from VPX_NON_ORM_VM_CONFIG_INFO where ID=<VM_ID>;
    
         delete from VPX_NORM_VM_FLE_FILE_INFO where VM_ID=<VM_ID>;
    
         delete from VPX_VDEVICE_BACKING_REL where VM_ID=<VM_ID>;
    
         delete from VPX_VIRTUAL_DISK_IOFILTERS where VM_ID=<VM_ID>;
    
         delete from VPX_VM_STATIC_OVERHEAD_MAP where VM_ID=<VM_ID>;
    
         delete from VPX_VM_TEXT where VM_ID=<VM_ID>;
    
         delete from VPX_VM where ID=<VM_ID>;
    
         delete from VPX_ENTITY where ID=<VM_ID>;
    
         delete from VPX_DVPORT where connectee='<VM_Name>';
  6. Start the vpxd service:
    bash
     
    service-control --start vmware-vpxd

 

 

Additional Information