Pages

Thursday, July 13, 2023

CLI Command to Delete EBS Volumes in bulk

 To list the Volumes

root@ip-10-251-0-29:~# aws ec2 describe-volumes --filters Name=status,Values=available --query 'Volumes[*].[VolumeId,CreateTime]' --output text --region eu-west-1  | head -10 

        vol-024ada1919d95751f   2023-03-30T08:51:45.082000+00:00

        vol-0b78b5399102720e4   2023-03-30T09:02:03.993000+00:00

        vol-07ee418b2372a8580   2023-03-30T09:02:04.017000+00:00

        vol-0f2d1d8cb7b49236b   2023-03-30T09:02:03.945000+00:00

        vol-09dffaac9e5597062   2023-03-30T09:02:04.110000+00:00

        vol-06ececed4952011ba   2023-03-30T09:02:03.980000+00:00

        vol-0fb364aa16d431757   2023-05-24T07:19:23.422000+00:00

        vol-06051fd7ab09fc67d   2023-05-24T07:25:28.149000+00:00

        vol-035cda60dceaed3dd   2023-05-24T07:31:32.937000+00:00


To Delete oldest 100 volumes

root@ip-10-251-0-29:~# aws ec2 describe-volumes --filters Name=status,Values=available --query 'Volumes[*].[VolumeId]' --output text --region eu-west-1  | head -100 | xargs -n 1 aws ec2 delete-volume --volume-id


To Delete oldest 10 Volumes {ignoring the least one volume}

root@ip-10-251-0-29:~# aws ec2 describe-volumes --filters Name=status,Values=available --query 'Volumes[*].[VolumeId]' --output text --region eu-west-1  | head -10 | tail -9 | xargs -n 1 aws ec2 delete-volume --volume-id




No comments:

Post a Comment