Looks ok except you can't delete all layouts, you must keep at least one paper space layout.
(foreach item (layoutlist) (vl-cmdf "._layout" "_delete" item))
//My code is looping through all the layouts in a DWG looking for one specific layout that I know the name of.
//All the other layouts are to be deleted.
DBDictionary layouts;
Database externalDatabase = new Database(false, true);
externalDatabase.ReadDwgFile(@"C:\Users\MyUser\Drawing.dwg", FileOpenMode.OpenForReadAndAllShare, true, "");
HostApplicationServices.WorkingDatabase = externalDatabase;
ObjectId modelSpaceId = LayoutManager.Current.GetLayoutId("Model");
using (var transaction = externalDatabase.TransactionManager.StartTransaction())
{
//In the method I use I have to set OpenMode.ForWrite
//but in most cases I guess OpenMode.ForRead would be enough
layouts = (DBDictionary)transaction.GetObject(externalDatabase.LayoutDictionaryId, OpenMode.ForWrite);
transaction.Commit();
transaction.Dispose();
}
foreach (DBDictionaryEntry layout in layouts)
{
if (String.Equals(layout.Key, "Model"))
{
continue;
}
else if (String.Equals(layout.Key, "Name Of Layout To Keep"))
{
continue;
}
else
{
LayoutManager.Current.SetCurrentLayoutId(modelSpaceId);
LayoutManager.Current.DeleteLayout(layout.Key);
LayoutManager.Current.SetCurrentLayoutId(modelSpaceId);
}
}
//Don't forget to switch HostApplicationServices.WorkingDatabase back to the original database