Skip to content

Instantly share code, notes, and snippets.

@yoimbert
Created July 22, 2014 08:08
Show Gist options
  • Save yoimbert/a03d991e934011c00055 to your computer and use it in GitHub Desktop.
Save yoimbert/a03d991e934011c00055 to your computer and use it in GitHub Desktop.
Clean up your AMI Snapshots
#source : http://kloudrocks.com/
# Unregister and clean AMI snapshots
$amiName = 'ami-XXXX' # replace this with the AMI ID you need to clean-up
$myImage = Get-EC2Image $amiName
$count = $myImage[0].BlockDeviceMapping.Count
# Loop and store snapshotID(s) to an array
$mySnaps = @()
for ($i=0; $i -lt $count; $i++)
{
$snapId = $myImage[0].BlockDeviceMapping[$i].Ebs | foreach {$_.SnapshotId}
$mySnaps += $snapId
}
# Perform the clean up
Write-Host "Unregistering" $amiName
Unregister-EC2Image $amiName
foreach ($item in $mySnaps)
{
Write-Host 'Removing' $item
Remove-EC2Snapshot $item
}
Clear-Variable mySnaps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment