I tried to delete my Amazon VPC, and I received a dependency error. How can I delete my Amazon VPC?

Last updated: 2019-09-11

I received a dependency error when I tried to delete my Amazon Virtual Private Cloud (Amazon VPC). How do I delete my Amazon VPC?

Short Description

If you use the AWS Command Line Interface (AWS CLI) to delete the Amazon VPC, you must first do the following:

  • Terminate all instances
  • Delete all subnets
  • Delete custom security groups and custom route tables
  • Detach any internet gateways or virtual private gateways

If you delete your Amazon VPC using the Amazon VPC console, all its components are also deleted. These components can include subnets, security groups, network access control lists (ACLs), route tables, internet gateways, virtual private gateways, VPC peering connections, or DHCP options. However, if you don't delete dependent resources before attempting to delete the Amazon VPC, you'll receive one of the dependency errors below.

Resolution

Resolve errors received when trying to delete an Amazon VPC through the AWS CLI

The following error indicates that there are dependencies that must be removed 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. Be sure to replace vpc-id with your VPC ID.

#!/bin/bash
vpc="vpc-xxxxxxxxxxxxx" 
aws ec2 describe-internet-gateways --filters 'Name=attachment.vpc-id,Values='$vpc | grep InternetGatewayId
aws ec2 describe-subnets --filters 'Name=vpc-id,Values='$vpc | grep SubnetId
aws ec2 describe-route-tables --filters 'Name=vpc-id,Values='$vpc | grep RouteTableId
aws ec2 describe-network-acls --filters 'Name=vpc-id,Values='$vpc | grep NetworkAclId
aws ec2 describe-vpc-peering-connections --filters 'Name=requester-vpc-info.vpc-id,Values='$vpc | grep VpcPeeringConnectionId
aws ec2 describe-vpc-endpoints --filters 'Name=vpc-id,Values='$vpc | grep VpcEndpointId
aws ec2 describe-nat-gateways --filter 'Name=vpc-id,Values='$vpc | grep NatGatewayId
aws ec2 describe-security-groups --filters 'Name=vpc-id,Values='$vpc | grep GroupId
aws ec2 describe-instances --filters 'Name=vpc-id,Values='$vpc | grep InstanceId
aws ec2 describe-vpn-connections --filters 'Name=vpc-id,Values='$vpc | grep VpnConnectionId
aws ec2 describe-vpn-gateways --filters 'Name=attachment.vpc-id,Values='$vpc | grep VpnGatewayId
aws ec2 describe-network-interfaces --filters 'Name=vpc-id,Values='$vpc | grep NetworkInterfaceId

2.    Delete the remaining dependencies 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 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 and then retry deleting the Amazon VPC.

The following error indicates that the Amazon VPC has network interfaces for other AWS services attached: "VPC contains one or more in-use network interfaces, and cannot be deleted until those network interfaces have been deleted." 

Follow these steps to remove the dependent services, and then retry deleting the Amazon VPC:

1.    Open the Amazon EC2 console.

2.    Select the Region that the Amazon VPC is in.

3.    In the navigation pane, under Network Interfaces, search for the VPC ID of the Amazon VPC that you're deleting.

4.    Select the elastic network interface and choose the Details tab.

5.    Review the Description to see which resources the interface is attached to.

6.    Delete the associated resources.
Note: If the network interface is a primary network interface, then it's deleted when you delete the instance.

The following errors indicate that the Amazon VPC has a dependency: 

  • "Network has some mapped public address(es). Please unmap those public address(es) before detaching the gateway." 
  • "VPC has one or more attached internet gateways or virtual private gateways, and cannot be deleted until they are detached or deleted." 

Follow these steps to remove the dependencies, and then retry deleting the Amazon VPC:

1.    Open the Amazon VPC console.

2.    Select the Region that the Amazon VPC is in.

3.    In the navigation pane, choose NAT Gateways or Virtual Private Gateways.

4.    Select the NAT gateway or virtual private gateway.

5.    Review the Description to see which resources the interface is attached to.

6.    For Actions, choose Delete NAT Gateway (for internet gateways) or Detach from VPC (for virtual private gateways).


Did this article help you?

Anything we could improve?


Need more help?