Word Automation Services: Deleting Source Files after Conversion using C#

This is the complete sample for deleting source files after conversion from the article Developing with SharePoint 2010 Word Automation Services.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Word.Server.Conversions;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        string siteUrl = "https://localhost";
        // If you manually installed Word Automation Services, then replace the name
        // in the following line with the name that you assigned to the service when
        // you installed it.
        string wordAutomationServiceName = "Word Automation Services";
        using (SPSite spSite = new SPSite(siteUrl))
        {
    Console.WriteLine("Starting conversion job");
            ConversionJob job = new ConversionJob(wordAutomationServiceName);
            job.UserToken = spSite.UserToken;
            job.Settings.UpdateFields = true;
            job.Settings.OutputFormat = SaveFormat.PDF;
            job.Settings.OutputSaveBehavior = SaveBehavior.AlwaysOverwrite;
            SPFolder folderToConvert = spSite.RootWeb.GetFolder("Shared Documents");
            job.AddFolder(folderToConvert, folderToConvert, false);
   job.Start();
            Console.WriteLine("Conversion job started");
            ConversionJobStatus status = new ConversionJobStatus(wordAutomationServiceName,
                job.JobId, null);
            Console.WriteLine("Number of documents in conversion job: {0}", status.Count);
            while (true)
            {
                Thread.Sleep(5000);
                status = new ConversionJobStatus(wordAutomationServiceName, job.JobId, null);
                if (status.Count == status.Succeeded + status.Failed)
                {
                    Console.WriteLine("Completed, Successful: {0}, Failed: {1}",
                        status.Succeeded, status.Failed);
                    Console.WriteLine("Deleting only items that successfully converted");
                    ReadOnlyCollection<ConversionItemInfo> convertedItems =
                        status.GetItems(ItemTypes.Succeeded);
                    foreach (var convertedItem in convertedItems)
                    {
                Console.WriteLine("Deleting item: Name:{0}", convertedItem.InputFile);
                        folderToConvert.Files.Delete(convertedItem.InputFile);
                    }
                    break;
                }
                Console.WriteLine("In progress, Successful: {0}, Failed: {1}",
                    status.Succeeded, status.Failed);
            }
        }
    }
}