While cleaning up by removing a couple of unused resources on an OpenStack server, I had considerably more difficulty than expected deleting a network that no longer had any instances using it.
The error appearing in the console was Unable to delete network reteKubeGiorgio. Most possible reason is because one or more ports still exist on the requested network....
I therefore tried to delete all the ports manually, but one of them seemed impossible to remove and returned a rather uninformative error instead.
What’s more, I did not know how to detach the network:ha_router_replicated_interface device that the port still associated itself with.
After losing hope of getting any result from the console alone, I decided to fall back to the command-line interface (CLI).
After creating the credentials and setting up the environment, a little online research combined with a few AI suggestions pointed me in the right direction.
The clean option
Ideally, removing a port should be straightforward with the following commands:
# Get the list of networks
openstack network list
# Get all the ports still attached to the network
openstack port list --network <network_id_or_name>
# Get all the information needed about the port
openstack port show <port_id>
# Detach the port from the router
openstack router remove port <device_id> <port_id>
# If it was the last remaining port, remove the network
openstack network delete <network_id_or_name>
When things go wrong
If, as in my case, attempting to remove the port produces the error <device-id> does not have an interface with id <port-id>, it is probably because that device no longer exists.
We can gently and manually make OpenStack infer this by running the following commands:
# Get the list of networks
openstack network list
# Get all the ports still attached to the network
openstack port list --network <network-id-or-name>
# If the previous approach did not work,
# manually change the port owner
openstack port set --device-owner "" <port_id>
# The port can now be deleted
openstack port delete <port_id>
# If it was the last remaining port, remove the network
openstack network delete <network_id_or_name>