I tried to delete my Amazon VPC, but I received a dependency error. How can I delete my Amazon VPC?
Last updated: 2022-07-26
I received a dependency error when I tried to delete my Amazon Virtual Private Cloud (Amazon VPC). How do I delete it?
When you delete an Amazon VPC, all its components are also deleted. These components can include subnets, security groups, network access control lists (network ACLs), route tables, internet gateways, and DHCP options. However, if you don't delete dependent resources before you attempt to delete your Amazon VPC, then you receive dependency errors.
Before you delete your VPC, you must delete the dependent resources.
Note: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent AWS CLI version.
Resolve errors received when trying to delete an Amazon VPC through the AWS CLI
The following error indicates that there are dependencies that you must remove before you can delete the Amazon VPC:
"An error occurred (DependencyViolation) when calling the DeleteVpc operation: The vpc 'vpc-id' has dependencies and cannot be deleted."
To resolve this error:
1. Run the following script to find the remaining dependencies:
#!/bin/bash vpc="vpc-xxxxxxxxxxxxx" region="yy-yyyy-y" aws ec2 describe-vpc-peering-connections --region $region --filters 'Name=requester-vpc-info.vpc-id,Values='$vpc | grep VpcPeeringConnectionId aws ec2 describe-nat-gateways --region $region --filter 'Name=vpc-id,Values='$vpc | grep NatGatewayId aws ec2 describe-instances --region $region --filters 'Name=vpc-id,Values='$vpc | grep InstanceId aws ec2 describe-vpn-gateways --region $region --filters 'Name=attachment.vpc-id,Values='$vpc | grep VpnGatewayId aws ec2 describe-network-interfaces --region $region --filters 'Name=vpc-id,Values='$vpc | grep NetworkInterfaceId
Note: In the preceding script, enter your VPC ID in the vpc field, and AWS Region (for example, us-east-1) in the Region field. Also, make sure that you're using credentials with appropriate permissions to run the Describe API calls.
2. Delete the remaining dependencies that you identified in step 1, and then retry deleting your Amazon VPC.
Resolve errors received when trying to delete an Amazon VPC through the Amazon VPC console
The following error indicates that the Amazon VPC has Amazon Elastic Compute Cloud (Amazon EC2) instances running on it: "VPC contains one or more instances, and cannot be deleted until those instances have been terminated." To resolve this error, terminate your instances.
The following error indicates that the Amazon VPC has network interfaces that are in use or available: "VPC contains one or more in-use network interfaces, and cannot be deleted until those network interfaces have been deleted."
The following error indicates that there are requester-managed network interfaces that you cannot delete: "An error occurred (InvalidParameterValue) when calling the DeleteNetworkInterface operation: Network interface 'eni-aabbccdd' is currently in use." To delete requester-managed network interfaces, you must delete the AWS service that created the network interfaces.
Follow these steps to remove the dependent services:
- Open the Amazon EC2 console.
- Select the AWS Region that the Amazon VPC is in.
- In the navigation pane, under Network Interfaces, search for the VPC ID of the Amazon VPC that you're deleting.
- Select the network interface and choose the Details tab.
- Review the Description to see which resources the network interface is attached to.
- Delete the associated resources. For example, if you are deleting a Network Interface and the Requester ID is 'amazon-elb," use the value in the Description field of the Elastic Network Interface to identify the Elastic Load Balancer. Then, navigate to the Load Balancer section of the Amazon EC2 Management console, locate the load balancer, and delete it.
Note: If the network interface is a primary one, then it's deleted when you delete the instance.
The following error indicates that the Amazon VPC has a dependency: "VPC has one or more attached NAT gateways or virtual private gateways, and cannot be deleted until they are detached or deleted." To delete them, follow these steps: Delete a NAT gateway and Detaching and deleting a virtual private gateway.
The following error indicates that there are active VPC peering connections in the Amazon VPC: "The VPC is peered as a requester with at least one other VPC through peering connections" To delete them, follow these steps: Delete a VPC peering connection.