Manually Remove disconnected vSphere template from vCenter

0

Recently, I got in a situation with a VMware vSphere template that left me in a precarious position in the lab environment.  In the haste of rebuilding a lab environment, I had vMotioned all VMs off an ESXi host in the environment, decommissioned the host and started a rebuild of ESXi using the new vSphere 7.0 Update 1 installation.  However, I didn’t realize there was a template that was associated with the decommissioned host.  This left me in a situation where I couldn’t remove the host from vCenter since it had an associated template and I couldn’t remove the template since the host was disconnected.  I couldn’t simply reconnect the host since I had already reloaded it. It would seem this is a catch 22 situation. However, let’s see how to manually remove disconnected vSphere template with the vCenter command line.

VMware vSphere Templates are handled differently than VMs

Templates are handled differently by vCenter than are VMs. In fact, templates are not registered on an ESXi host the same way as a VM. Templates are a vCenter construct that is maintained and tracked by vCenter instead of being housed on the ESXi host. In other words, vSphere templates are housed in the vCenter database, aka PostgreSQL.

As mentioned before, I was in a catch 22. Official VMware documentation notes that if you have a disconnected template that you can simply reconnect the host into the cluster. Other posts have seen on this topic mentions that if you just remove the ESXi host, the disconnected template would go away. However, I didn’t find this to be the case, at least in vCenter Server 7.0 Update 1. I received an error trying to remove the disconnected host that was assigned the disconnected template. I also had vSphere Distributed Switches in play as well which helped to complicate matters.

Manually-Remove-disconnected-vSphere-template-from-vCenter Manually Remove disconnected vSphere template from vCenter
Manually Remove disconnected vSphere template from vCenter

Manually Remove disconnected vSphere template from vCenter

When it comes to removing disconnected vSphere templates from your vSphere inventory, the vCenter PostgreSQL command line is your friend here. Since the vSphere template is a vCenter construct, it lives in the database as mentioned earlier. Let’s take a look at the steps to remove a template from vCenter PostgreSQL DB.

The first thing you will need to do is login to the vCenter PostgreSQL DB. To do that, type the following. This still works in vCenter Server 7.0 Update 1.

/opt/vmware/vpostgres/current/bin/psql -d VCDB -U postgres

Now that you have logged into the vCenter Server PostgreSQL DB, you can look for the vSphere template entry by using the following:

SELECT id FROM vpx_entity WHERE name = 'my template name';

Note the ID that is presented for the template. Once you have the ID for the template in the vCenter Server PostgreSQL DB, you can delete the template from the database.

To delete the disconnected vSphere template, use the following command. You will use the ID that you found by the command above to perform the DELETE statement.

DELETE FROM vpx_entity WHERE id='12345';
Deleting-a-disconnected-vSphere-template-from-the-vCenter-Server-PostgreSQL-DB Manually Remove disconnected vSphere template from vCenter
Deleting a disconnected vSphere template from the vCenter Server PostgreSQL DB

After you run the above command, you can restart the vmpxa service on the vCenter Server. Historically, the command is:

service vmware-vpxa restart

You can also use the VAMI interface for the vCenter Server to restart services as well.

Concluding Thoughts

If you get into a situation with a stubborn vSphere template that is causing you headaches with your vSphere environment, the command line is your friend. A state where the vSphere template is diconnected and even the host that houses the template is disconnected will leave you in a catch 22 of not really being able to do anything with either template or the host in the vSphere Client. However, using the vCenter Server command line you can manually remove disconnected vSphere template from vCenter to delete the template from PostgreSQL which allows you to resolve the situation.

StarWind VSAN