I wrote this a while back, and I thought it was generic enough that someone else might find it useful.
using System;
using System.Text;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
[assembly: ExtensionApplication(typeof(cgabriel.PurgeTools))]
[assembly: CommandClass(typeof(cgabriel.PurgeTools))]
namespace cgabriel
{
public class PurgeTools : IExtensionApplication
{
public void Initialize() { }
public void Terminate() { }
public static bool purgeItems(Database db, ObjectIdCollection tableIds, bool silent)
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
bool itemsPurged = false;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ObjectIdCollection purgeableIds = new ObjectIdCollection();
foreach (ObjectId tableId in tableIds)
{
SymbolTable table = (SymbolTable)tr.GetObject(tableId, OpenMode.ForRead, false);
foreach (ObjectId recordId in table)
purgeableIds.Add(recordId);
}
db.Purge(purgeableIds);
if (purgeableIds.Count != 0)
{
itemsPurged = true;
foreach (ObjectId id in purgeableIds)
{
SymbolTableRecord record = (SymbolTableRecord)tr.GetObject(id, OpenMode.ForWrite);
string recordName = record.Name;
if (!silent)
{
if (!recordName.Contains("|"))
{
ed.WriteMessage("\nPurging " +
record.GetType().Name + " " + recordName);
}
}
record.Erase();
}
}
tr.Commit();
}
return itemsPurged;
}
public static bool purgeAll(Database db, bool silent)
{
ObjectIdCollection tableIds = new ObjectIdCollection();
tableIds.Add(db.BlockTableId);
tableIds.Add(db.DimStyleTableId);
tableIds.Add(db.LayerTableId);
tableIds.Add(db.LinetypeTableId);
tableIds.Add(db.RegAppTableId);
tableIds.Add(db.TextStyleTableId);
tableIds.Add(db.UcsTableId);
tableIds.Add(db.ViewportTableId);
tableIds.Add(db.ViewTableId);
return purgeItems(db, tableIds, silent);
}
[CommandMethod("PurgeTools", "PurgeAll", CommandFlags.Modal | CommandFlags.DocExclusiveLock)]
public static void purgeAll()
{
while (purgeAll(Application.DocumentManager.MdiActiveDocument.Database, false))
continue;
}
}
}