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.
A virtual machine cannot be removed from the inventory.
The "Remove from Inventory" option is grayed out.
The virtual machine appears as "orphaned" or "inaccessible" in the vSphere Client.
Attempts to delete a VM template result in the following error: YYYY-MM-DDT HH:MM:SS The object 'vim.VirtualMachine:vm-####' has already been deleted or has not been completely created.
VMware vCenter Server 8.x
VMware vCenter Server 7.x
Metadata inconsistency occurs when a virtual machine is removed directly from an ESXi host during vCenter Server downtime or when an administrative task is interrupted, leaving residual entries in the vCenter Server database.
NOTE: Ensure a fresh backup or offline snapshot of the vCenter Server Appliance has been created. 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
Note the VM ID of the VM(s) to be removed. To get the VM ID, select the VM object in the vCenter UI and note it from the URL (format vm-####) or find it in the vpxd.log (var/log/vmware/vpxd/vpxd.log) on the vCenter when searching for the VM name.
Stop the vpxd service: service-control --stop vmware-vpxd
Connect to the database: /opt/vmware/vpostgres/current/bin/psql -d VCDB -U postgres
Execute the following DELETE statements in order, replacing <VM_ID> with the numeric ID from the VM ID noted and <VM_Name> with the full "vm-####":
|
For scenarios involving multiple stale objects, a script can automate the deletion process.
Gather IDs: Identify the IDs of the stale objects from the database: select id, name from vpx_entity where name like '%pattern%';
Create the Script:
Open a new file: vi /tmp/delete_vms.sh
Paste the following content, replacing x, y, z with your comma-separated VM IDs:
|
Set permissions: chmod +x /tmp/delete_vms.sh
Run the script: /tmp/delete_vms.sh >> /tmp/removal_results.txt
service-control --start vmware-vpxdVPX_DVPORT entry is removed to prevent port conflicts.