Synopsis
Cloud Director specifically requires devices to be gracefully removed from itself prior to removing the devices from the vCenter or physically removing them from the DC. If you have this issue then it’s best to see if you can power back on devices or restore the VMs and then remove them in Cloud Director properly as that is the best method and does not require database manipulation. If you do not have that option then you will need to raw edit the postgres DB.
NOTE: Always be sure you have a fresh backup and/or snapshot of your Cloud Director VM before proceeding.  If you do not understand these commands nor have experience with database work then I highly suggest creating an SR with VMware and letting them work with you.
Procedure
1. Get into postgres service (This changes the prompt to postgres@director [ /root ]$ ):
 “`
 su postgres
 “`
1. Connect to SQL service (This changes the prompt to postgres=#):
 “`
 psql
 “`
1. Set instance in Postgres (This changes the prompt to vcloud=#):
 “`
 c vcloud
 “`
1. Enable expanded display:
 “`
 x
 “`
1. Display information looking for specific target VM names repeat for all VMs in question by running a select command (Where VM_name% is the VM name we’re looking for):
 “`
 select * from vapp_vm where name  like ‘VM_name%’;
 “`
 Examples:
 “`
 select * from vapp_vm where name  like ‘Stale-VM-001%’;
 Output:
 -[ RECORD 1 ]—————–+————————————-
 id                            | 01234567-abcd-1234-5678-901234567890
 vapp_scoped_vm_id             | Stale-VM-001
 vapp_id                       | a1234567-abcd-1234-5678-90123456789b
 creation_status               | DELETING_CONTENTS
 name                          | Stale-VM-001
 descr                         |
 is_autoconfig                 | f
 boot_order                    | 0
 boot_delay                    | 0
 boot_action                   | 1
 stop_delay                    | 0
 stop_action                   | 2
 version_number                | 6
 storage_class_lr_id           | c1234567-abcd-1234-5678-90123456789d
 cvm_id                        | e1234567-abcd-1234-5678-90123456789f
 nvm_id                        | g1234567-abcd-1234-5678-90123456789h
 svm_id                        | i1234567-abcd-1234-5678-90123456789j
 ovf_env                       |
 ovf_env_transports            |
 needs_customization           | t
 computer_name                 | Stale-VM-001
 default_storage_class_name    |
 last_marker                   |
 date_created                  | 1975-06-22 08:30:46.995
 memory_configured_mb          | 4096
 check_post_gc_status          | f
 vdc_compute_policy_id         | k1234567-abcd-1234-5678-90123456789l
 vcpu_speed_mhz                |
 vm_sizing_policy_id           |
 is_compute_policy_compliant   | t
 is_compute_policy_immutable   | f
 is_vm_sizing_policy_immutable | f
 “`
 “`
 select * from vapp_vm where name  like ‘Stale-VM-002%’;
 Output:
 -[ RECORD 1 ]—————–+————————————————————-
 id                            | abcdefgh-1234-abcd-efgh-abcdefghijkl
 vapp_scoped_vm_id             | Stale-VM-002
 vapp_id                       | 1bcdefgh-1234-abcd-efgh-abcdefghijk2
 creation_status               | DELETING_CONTENTS
 name                          | Stale-VM-002
 descr                         | VM: “Stale-VM-002” imported from VC as Discovered-VApp”
 is_autoconfig                 | f
 boot_order                    | 0
 boot_delay                    | 0
 boot_action                   | 1
 stop_delay                    | 0
 stop_action                   | 2
 version_number                | 6
 storage_class_lr_id           | c1234567-abcd-1234-5678-90123456789d
 cvm_id                        | abcdefgh-1234-abcd-efgh-abcdefghijkl
 nvm_id                        | 3bcdefgh-1234-abcd-efgh-abcdefghijk4
 svm_id                        | 5bcdefgh-1234-abcd-efgh-abcdefghijk6
 ovf_env                       |
 ovf_env_transports            |
 needs_customization           | t
 computer_name                 | VM-001
 default_storage_class_name    |
 last_marker                   |
 date_created                  | 1975-06-22 08:30:46.995
 memory_configured_mb          | 0
 check_post_gc_status          | f
 vdc_compute_policy_id         |
 vcpu_speed_mhz                |
 vm_sizing_policy_id           |
 is_compute_policy_compliant   | f
 is_compute_policy_immutable   | f
 is_vm_sizing_policy_immutable | f
 “`
1. In each record output you will need to copy the id section out and then run the following to delete the VMs:
 “`
 delete from vapp_eula where vm_id in (’e1234567-abcd-1234-5678-90123456789f’,'abcdefgh-1234-abcd-efgh-abcdefghijkl’);
 delete from vapp_vm_sclass_metrics where vapp_vm_id  in (’e1234567-abcd-1234-5678-90123456789f’,'abcdefgh-1234-abcd-efgh-abcdefghijkl’);
 delete from vm_gosc_status where vapp_vm_id  in (’e1234567-abcd-1234-5678-90123456789f’,'abcdefgh-1234-abcd-efgh-abcdefghijkl’);
 delete from guest_personalization_info where vapp_vm_id  in (’e1234567-abcd-1234-5678-90123456789f’,'abcdefgh-1234-abcd-efgh-abcdefghijkl’);
 delete from vapp_vm_disk_storage_class where vapp_vm_id  in (’e1234567-abcd-1234-5678-90123456789f’,'abcdefgh-1234-abcd-efgh-abcdefghijkl’);
 delete from vapp_product_info where vm_id  in (’e1234567-abcd-1234-5678-90123456789f’,'abcdefgh-1234-abcd-efgh-abcdefghijkl’);
 delete from vapp_vm where id in (’e1234567-abcd-1234-5678-90123456789f’,'abcdefgh-1234-abcd-efgh-abcdefghijkl’);
 “`
1. Then you will need get the ‘vapp_id’ information from the previous select statements and run the following commands to see if there are vApps still existing that contained the VMs:
 “`
 select * from vm_container where sg_id in (’a1234567-abcd-1234-5678-90123456789b’,'1bcdefgh-1234-abcd-efgh-abcdefghijk2′);
 Output:
 -[ RECORD 1 ]————-+———————————————————————————————————
 sg_id                     | a1234567-abcd-1234-5678-90123456789b
 name                      | Discovered Stale-VM-001-1a2b3c4d-1a2b-3c4d-5e6f-1a2b3c4d5e6f
 descr                     | VM: “Stale-VM-001” imported from VC as Discovered-VApp”
 user_id                   | a1b2c3d4-a1b2-a1b2-a1b2-a1b2c3d4e5f6
 sg_type                   | 1
 shared_count              | 0
 is_enabled                | t
 date_created              | 1975-06-22 08:30:46.995
 honor_boot_order          | f
 vmfolder_moref            | group-v1520
 vmfolder_vc_id            | 98765432-1098-7564-3210-abcdefghijkl
 auto_undeploy_ticks       |
 auto_undeploy_date        |
 auto_delete_ticks         |
 auto_delete_date          |
 is_auto_undeploy_notified | f
 is_auto_delete_notified   | f
 org_id                    | afafafaf-afaf-egeg-afaf-afafegegafaf
 org_vdc_id                | sdsdsdsd-sdsd-wdwd-sdsd-sdsdsdsdsdsd
 is_gold_master            | f
 fence_must_be             |
 version_number            | 9
 creation_status           | DELETING_CONTENTS
 transfer_session_id       |
 is_manifest_required      | f
 uniquename                | afafafaf-afaf-egeg-afaf-afafegegafaf|Discovered Stale-VM-001-1a2b3c4d-1a2b-3c4d-5e6f-1a2b3c4d5e6f
 customize_on_instantiate  | f
 is_deployed               | t
 in_maintenance_mode       | f
 auto_nature               | t
 linked_vapp_id            |
 is_generated_by_move      | f
 -[ RECORD 2 ]————-+———————————————————————————————————
 sg_id                     | 1bcdefgh-1234-abcd-efgh-abcdefghijk2
 name                      | Stale-VM-002-dcdcdcdc-dcdc-1234-dcdc-dcdcdcdcdcdc
 descr                     | VM: “Stale-VM-002” imported from VC as Discovered-VApp”
 user_id                   | a1b2c3d4-a1b2-a1b2-a1b2-a1b2c3d4e5f6
 sg_type                   | 1
 shared_count              | 0
 is_enabled                | t
 date_created              | 1975-06-22 08:30:46.995
 honor_boot_order          | f
 vmfolder_moref            | group-v2670
 vmfolder_vc_id            | 98765432-1098-7564-3210-abcdefghijkl
 auto_undeploy_ticks       |
 auto_undeploy_date        |
 auto_delete_ticks         |
 auto_delete_date          |
 is_auto_undeploy_notified | f
 is_auto_delete_notified   | f
 org_id                    | afafafaf-afaf-egeg-afaf-afafegegafaf
 org_vdc_id                | sdsdsdsd-sdsd-wdwd-sdsd-sdsdsdsdsdsd
 is_gold_master            | f
 fence_must_be             | f
 version_number            | 11
 creation_status           | DELETING_CONTENTS
 transfer_session_id       |
 is_manifest_required      | f
 uniquename                | afafafaf-afaf-egeg-afaf-afafegegafaf|Stale-VM-002-mnmnmnmn-mnmn-mnmn-1234-mnmnmnmnmnmn
 customize_on_instantiate  | f
 is_deployed               | t
 in_maintenance_mode       | f
 auto_nature               | f
 linked_vapp_id            |
 is_generated_by_move      | f
 “`
1. Now we need to delete those vApps:
 “`
 delete from vm_container where sg_id in (’abcdefgh-1234-abcd-efghi-abcdefghijkl’,'1bcdefgh-1234-abcd-efgh-abcdefghijk2′);
 Output:
 DELETE 2
 “`
Other queries
1. Return information for a provider VDC, example is DFW:
 “`
 select * from prov_vdc where name like ‘%DFW%’;
 Output:
     (0 rows)
 “`
1. Return ALL provider VDCs:
 “`
 select * from prov_vdc;
 “`
1. To Exit type:
 “`
 q
 “`