$instanceId = $instance['InstanceId']; } else { $instance = $clientHelper->getInstanceById($instanceId); } $instanceName = $clientHelper->resolveInstanceName($instance); $console->writeLine("Pruning snapshots on {$instanceName} older than " . $maxAge->format('c')); $volumes = $clientHelper->getVolumesByInstance($instanceId); if (count($volumes) === 0) { throw new Exception('No volumes found'); } $volumeIds = array(); foreach ($volumes as $volume) { array_push($volumeIds, $volume['VolumeId']); } $console->writeLine("Found " . count($volumeIds) . " volumes", Color::GRAY); $snapshots = $clientHelper->getCompleteSnapshotsForVolumes($volumeIds); $console->writeLine("Found " . count($snapshots) . " snapshots", Color::GRAY); $toDelete = array(); foreach ($snapshots as $snapshot) { $created = new DateTime($snapshot['StartTime']); if ($created < $maxAge) { array_push($toDelete, $snapshot['SnapshotId']); } } $console->writeLine("Found " . count($toDelete) . " old snapshots", Color::YELLOW); foreach ($toDelete as $snapshotId) { $console->writeLine('Deleting' . ($dryRun ? ' (dry-run) ' : ' ') . $snapshotId, Color::GRAY); if (!$dryRun) { $client->deleteSnapshot(array('SnapshotId' => $snapshotId)); } }