Clean an Azure Media Services account from .NET

When we are developing, a method that you cannot forget is one to remove all assets, locators, jobs and access policies that we have accumulated after extensive testing. The truth is it’s very simple, since we only go every list and delete the contents of them through the Delete method of each of the objects (also available DeleteAsync):

using Microsoft.WindowsAzure.MediaServices.Client;
using System;
using System.Configuration;

namespace CleanAMSAccount
{
    class Program
    {
        static void Main(string[] args)
        {
            var context = new CloudMediaContext(ConfigurationManager.AppSettings["AccountName"], ConfigurationManager.AppSettings["AccountKey"]);

            Console.WriteLine("Removing programs");

            foreach (var program in context.Programs)
            {
                if (program.State != ProgramState.Stopped)
                    program.Stop();
                program.Delete();
            }

            Console.WriteLine("Removing channels");

            foreach (var channel in context.Channels)
            {
                if (channel.State != ChannelState.Stopped)
                    channel.Stop();
                channel.Delete();
            }

            Console.WriteLine("Removing streaming units");

            foreach (var endpoint in context.StreamingEndpoints)
            {
                if (endpoint.State != StreamingEndpointState.Scaling && endpoint.ScaleUnits > 0)
                    endpoint.Scale(0);
            }

            Console.WriteLine("Removing encoding units");

            foreach (var endpoint in context.EncodingReservedUnits)
            {
                endpoint.CurrentReservedUnits = 0;
                endpoint.Update();
            }

            Console.WriteLine("Removing channels");

            foreach (var channel in context.Channels)
            {
                channel.Stop();
                channel.Delete();
            }


            Console.WriteLine("Cleaning jobs");

            foreach (var job in context.Jobs)
            {
                job.Delete();
            }

            Console.WriteLine("Cleaning locators");

            foreach (var locator in context.Locators)
            {
                locator.Delete();
            }

            Console.WriteLine("Cleaning access policies");

            foreach (var accessPolicy in context.AccessPolicies)
            {
                accessPolicy.Delete();
            }

            Console.WriteLine("Cleaning assets");

            foreach (var asset in context.Assets)
            {
                asset.Delete();
            }

            Console.WriteLine("Done!");
            Console.ReadLine();
        }
    }
}

Hope this helps.

Cheers!