Graphically deregister AWS AMIs and associated S3 snapshots

powershelllogoPreviously, I posted a PowerShell script that uses the AWS PowerShell cmdlets to deregister Amazon Machine Images (AMI) and associated S3 snapshots.

This post improves on that effort by using the PowerShell Out-GridView cmdlet to give you a sortable, filterable, graphical view in which to select one or more AMIs to delete (or “deregister” in AWS’s terminology). For each AMI you select, all the associated S3 snapshots are also deleted. It’s this latter capability I find so useful. Out of the box, the AWS console provides no automated way to delete the S3 snapshots that are associated with an AMI. It’s up to you to clean them up, if you wish to. Some may want to keep them (they can be used to recreate EBS volumes as one can with any S3 snapshot of an EBS volume). But I use AMIs as point-in-time snapshots of an instance so my preference is to delete AMI-associated snapshots when the AMI is deleted. If I want a snapshot of a volume, I take it separately. (BTW, here’s the script I use to create and automatically tag both AMIs and their S3 snapshots.)

When you run this script, it produces an Out-GridView window like this one (click on the image to enlarge; I blurred client-specific text):

Select AMIs to be deregistered (deleted)
Select AMIs to be deregistered (deleted)

In the list, you can use any kind of selection you like using standard Windows mouse and keyboard combinations. Click at the top of the list, hold down the shift key, scroll to the bottom and select the last entry to select all AMIs to be deleted. Or, using the keyboard, type Ctrl-A to select all AMIs. Use Ctrl-click to select specific AMIs in the list. Just about every selection technique works.

Best of all (and a big enhancement over my previous script to delete AMIs and S3 snapshots), Out-GridView allows you to sort and filter the results. As usual, click on the column headers to sort. And check this out: you can specify multiple filters for the columns displayed in the grid. Here’s a screenshot with a filter returning two different AMis by two different names:

Using Out-GridView filtering
Using Out-GridView filtering (click to enlarge)

Out-GridView is like a “live” Excel sheet. You can search (filter) and sort any way you want.

Once you’ve selected the AMIs you wish to deregister, just click OK. You’ll be prompted once more for each selected AMI –just in case you selected the wrong AMI or you change your mind.

Here’s the code. I hope you find it useful. The coolest thing about this is that it’s really only one PowerShell statement. That’s the “power of the pipeline”.

Let me know in the comments section how this works for you. Good luck.

     Created on:    4/18/2016 14:00
     Created by:    Alex Neihaus
     Organization:  Air11 Technology LLC
        (c) 2016 Air11 Technology LLC
        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at

        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License. #>

<# This script lists (via Out-GridView) all the AMI owned by the AWS account whose credentials are either specified (or, as assumed here, defaulted).
    The user can select (multi-select with standard Windows controls like Ctrl-click, Shift-Click, Ctrl-A etc.) the AMIs to be deleted. The script
    then de-registers the AMI and removes any associated S3 snapshots.

Import-Module AWSPowerShell
Get-EC2Image -Owner self | Select-Object -Property Imageid, Name, Description, CreationDate, Tag, BlockDeviceMapping | Out-GridView -PassThru -Title 'Delete AMIs and associated snapshots' | `
  #Out-GridView with -PassThru puts selected properties onto pipeline

ForEach-Object {
    $AMI_ImageID = $_.ImageID
    $AMI_Name = $_.Name
    $AMI_Description = $_.Description
    $snapshots = @($_.BlockDeviceMapping.ebs.snapshotid) 
    $title = "Bulk delete AMIs"
    [string]$message = $AMI_Name + "`r`n" + $AMI_ImageId + "`r`n" + $AMI_Description + "`r`n" + "`r`n" + 'ARE YOU SURE?'
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription '&Yes', 'Deletes AMI and snapshots'
    $no = New-Object System.Management.Automation.Host.ChoiceDescription '&No', 'Ignores AMI and snapshots'
    $options = [System.Management.Automation.Host.ChoiceDescription[]] ($yes, $no)
    $result = $host.ui.PromptForChoice($title, $message, $options, 0)
    switch ($result)
        0 {
            Unregister-EC2Image -ImageId $AMI_ImageId -Force # Snapshots of root volumes cannot be deleted unless the AMI is deregistered first
            Write-Host $AMI_ImageId "Unregistered"
            foreach ($SnapshotID in $snapshots)
                Remove-EC2Snapshot $SnapshotID -Force # Delete leftover snapshots
                Write-Host $SnapshotID "Removed"
        1 { Write-Host $AMI_ImageID "NOT deleted" }




, , ,



Leave a Reply

Your email address will not be published. Required fields are marked *