# Delete layouts

**URL:** <https://forum.dynamobim.com/t/delete-layouts/94601>\
**Category:** Civil 3D\
**Created:** [October 16, 2023, 2:30pm UTC](https://forum.dynamobim.com/t/delete-layouts/94601 "2023-10-16T14:30:37Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![p.da](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@p.da](https://forum.dynamobim.com/u/p.da)\
**Post date:** [October 16, 2023, 2:30pm UTC](https://forum.dynamobim.com/t/delete-layouts/94601/1 "2023-10-16T14:30:37Z")

</div>

Hello, I’m trying create script, which will be delete all layout excluding layout name the same as file name. How I can delete layout by dynamo from Civil 3d?

---

<div class="post-metadata">

**Author:** ![patrick.ericson](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/patrick.ericson/32/121411_2.png) [@patrick.ericson](https://forum.dynamobim.com/u/patrick.ericson)\
**Post date:** [October 16, 2023, 4:34pm UTC](https://forum.dynamobim.com/t/delete-layouts/94601/2 "2023-10-16T16:34:00Z")

</div>

What have you tried so far?

When I tested on my own I could filter out the Layout except the one with the same name as file name byt couldn’t delete the rest.

---

<div class="post-metadata">

**Author:** ![patrick.ericson](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/patrick.ericson/32/121411_2.png) [@patrick.ericson](https://forum.dynamobim.com/u/patrick.ericson)\
**Post date:** [October 16, 2023, 5:38pm UTC](https://forum.dynamobim.com/t/delete-layouts/94601/3 "2023-10-16T17:38:51Z")

</div>

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/e/aeb8f61fe1e0e5cc02c9d9e37eb6f4ca4d15d9e6.png)

---

<div class="post-metadata">

**Author:** ![p.da](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@p.da](https://forum.dynamobim.com/u/p.da)\
**Post date:** [October 16, 2023, 8:17pm UTC](https://forum.dynamobim.com/t/delete-layouts/94601/4 "2023-10-16T20:17:25Z")

</div>

Yes, thaks You. But I don’t know, how can I delete this layouts from files?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/9/19435ad532b83f3a5d682b030af84a99eb30b6d4.png)

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [October 17, 2023, 12:27am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/5 "2023-10-17T00:27:38Z")

</div>

You can use python by ‘side loading’ the file as a Database and erasing the layout using the AutoCAD API. Use carefully as this will overwrite files - I have added a boolean to `IN[2]` specifically to try a dry run before committing. **EDIT** See updated code below as this was producing unhandled errors

```python
import clr
import traceback
from pathlib import Path # python 3.4+

clr.AddReference("AcDbMgd")
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.DatabaseServices import *

from System.IO import FileShare

dwgdir = IN[0]
layouts = IN[1] if isinstance(IN[1], list) else [IN[1]] # list of layouts to erase
delete_layouts = IN[2] # True / False

files = list(map(str, Path(dwgdir).glob("*.dwg")))

output = []

for dwg in files:
    info = []
    with Database(False, True) as db:
        db.ReadDwgFile(dwg, FileShare.ReadWrite, False, "")
        info.append(dwg)
        with db.TransactionManager.StartTransaction() as t:
            try:
                layoutdict = t.GetObject(db.LayoutDictionaryId, OpenMode.ForRead)
                for layout in layoutdict.get_Keys():
                    if layout in layouts:
                        layout_to_erase = t.GetObject(layoutdict[layout], OpenMode.ForWrite)
                        if delete_layouts:
                            layout_to_erase.Erase()
                        info.append(f"{layout} erased")
                        
            except:
               output.append(traceback.format_exc())
               
            t.Commit()
            
        if delete_layouts and len(info) > 1: # Layout(s) erased
            db.SaveAs(dwg, DwgVersion.Current)
            
    output.append(info)
    
OUT = output

```

---

<div class="post-metadata">

**Author:** ![patrick.ericson](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/patrick.ericson/32/121411_2.png) [@patrick.ericson](https://forum.dynamobim.com/u/patrick.ericson)\
**Post date:** [October 17, 2023, 3:23am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/6 "2023-10-17T03:23:32Z")

</div>

Thanks Mike.  
I’m new to Python so this was a good example how to use it.

---

<div class="post-metadata">

**Author:** ![p.da](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@p.da](https://forum.dynamobim.com/u/p.da)\
**Post date:** [October 17, 2023, 4:48am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/7 "2023-10-17T04:48:32Z")

</div>

I dont know how to use it?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/7/77b7cc8174d39738bc7c418f9e129847931a3a38.png)  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/3/c3461b6f0e30238845451be3f7d6d037a2c0e86e.png)

---

<div class="post-metadata">

**Author:** ![p.da](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@p.da](https://forum.dynamobim.com/u/p.da)\
**Post date:** [October 17, 2023, 6:59am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/8 "2023-10-17T06:59:42Z")

</div>

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/7/4/74b54c9c6af0bcbe0157e8d10951bd4532c63280.png)

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [October 17, 2023, 8:31am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/9 "2023-10-17T08:31:05Z")

</div>

Hi Piotr,  
I was getting a lot of unhandled exceptions on my code leading to C3D crashing (this is a professional hazard when working with the AutoCAD API). Anyhow… I have updated the code as follows

```python
import clr
import traceback
from pathlib import Path # python 3.4+

clr.AddReference("AcDbMgd")
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.DatabaseServices import *

from System.IO import FileShare

dwgdir = IN[0]
layouts = IN[1] if isinstance(IN[1], list) else [IN[1]]
delete_layouts = IN[2] # True / False

files = list(map(str, Path(dwgdir).glob("*.dwg")))

# https://forums.autodesk.com/t5/net/remove-layout/m-p/5824156/highlight/true#M46014

output = []

currentdb = HostApplicationServices.WorkingDatabase

for dwg in files:
    info = []
    with Database(False, True) as db:
        db.ReadDwgFile(dwg, FileShare.ReadWrite, False, "")

        HostApplicationServices.WorkingDatabase = db
        layoutmanager = LayoutManager.Current

        info.append(dwg)

        for layout in layouts:
            try:
                if delete_layouts:
                    layoutmanager.DeleteLayout(layout)
                info.append(f"{layout} erased")
            except:
                info.append(traceback.format_exc())

        if delete_layouts and len(info) > 1: # Layout(s) erased
            db.SaveAs(dwg, DwgVersion.Current)

    output.append(info)

HostApplicationServices.WorkingDatabase = currentdb

OUT = output

```

It’s based on a [response](https://forums.autodesk.com/t5/net/remove-layout/m-p/5824156/highlight/true#M46014) by [Gilles Chanteau](https://forums.autodesk.com/t5/user/viewprofilepage/user-id/109424) who is unparalleled in their knowledge of the API  
Inputs as follows:

- `IN[0]` Path to directory with dwg files
- `IN[1]` Layouts to delete
- `IN[2]` Dry run (False) Do it! (True)

You could use a _List.UniqueItems_ node on the flattened layout list to reduce iterations  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/5/0529359c1605b3d5cf2481a62bd0b5a8ad19074d.png)

---

<div class="post-metadata">

**Author:** ![p.da](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@p.da](https://forum.dynamobim.com/u/p.da)\
**Post date:** [October 17, 2023, 6:47pm UTC](https://forum.dynamobim.com/t/delete-layouts/94601/10 "2023-10-17T18:47:46Z")

</div>

It works! Thanks, you are great!

---

<div class="post-metadata">

**Author:** ![p.da](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@p.da](https://forum.dynamobim.com/u/p.da)\
**Post date:** [October 18, 2023, 6:26am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/11 "2023-10-18T06:26:52Z")

</div>

Unfortunately, script works only at test clean files. Scrip causes crash Civil 3d and damaged files. I tried on Civil 2022 and 2024 and many DWG. After use script DWG has 0KB.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/0/60c7f508f92692f9613d92e20b30b0f2aabaa336.png)

---

<div class="post-metadata">

**Author:** ![y.srewil](https://avatars.discourse-cdn.com/v4/letter/y/e9a140/32.png) [@y.srewil](https://forum.dynamobim.com/u/y.srewil)\
**Post date:** [October 18, 2023, 7:30am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/12 "2023-10-18T07:30:43Z")

</div>

Did you test Object.Delete node? you can remove a list of objects using it!  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/5/b5900766dc7932aa1477ad8d3c4cb83da11c2dd3.png)

---

<div class="post-metadata">

**Author:** ![patrick.ericson](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/patrick.ericson/32/121411_2.png) [@patrick.ericson](https://forum.dynamobim.com/u/patrick.ericson)\
**Post date:** [October 18, 2023, 9:18am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/13 "2023-10-18T09:18:41Z")

</div>

I tested that one but it didn’t work for layouts

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [October 18, 2023, 10:25am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/14 "2023-10-18T10:25:22Z")

</div>

I have it working on C3D 2022. Start by starting with a clean graph and testing, testing, testing.

Of my AutoCAD API scripts which do complicated actions some have taken hundreds of hours of testing, often crashing C3D and often trying different order of actions, using different methods etc.

You will have to dig into the [API docs](https://help.autodesk.com/view/OARX/2024/ENU/?guid=OARX-ManagedRefGuide-What_s_New) and persevere

I know it may not provide solace to you at the moment, however we are able to do large process runs on DWGs for issue where we swap blocks, delete layouts, change text, embed images, zoom views, add XData etc. Unfortunately I can’t share those scripts.

---

<div class="post-metadata">

**Author:** ![jbrunkhorst](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@jbrunkhorst](https://forum.dynamobim.com/u/jbrunkhorst)\
**Post date:** [February 11, 2025, 1:34pm UTC](https://forum.dynamobim.com/t/delete-layouts/94601/15 "2025-02-11T13:34:13Z")

</div>

@Mike.Buttery Do you think it would be easy to tweak the python script to work for the current file rather than a whole directory path?

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [February 19, 2025, 12:36am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/16 "2025-02-19T00:36:28Z")

</div>

You could work on the current file, however the strategy is slightly different as there is no side loading.

I would first try it with Dynamo nodes rather than python as I would expect that to be more stable

---

<div class="post-metadata">

**Author:** ![jbrunkhorst](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@jbrunkhorst](https://forum.dynamobim.com/u/jbrunkhorst)\
**Post date:** [February 19, 2025, 2:12am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/17 "2025-02-19T02:12:40Z")

</div>

@Mike.Buttery  
Interesting, so it would be advantageous to run this externally? I can tweak my workflow to do it that way. I feel like I am getting really close with some python scripts to get my ultimate goal of breaking out a master file of a bunch of layouts into separate .dwg’s (either single layout per .dwg or grouping layouts together into a single .dwg), but it seems to crash CAD on me once I get close. I am open to any ideas on how to accomplish this.

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [February 19, 2025, 5:55am UTC](https://forum.dynamobim.com/t/delete-layouts/94601/18 "2025-02-19T05:55:31Z")

</div>

You could make a copy of the current dwg using `Database.Wblock()` method and modifying as required and then use `SaveAs()`. See [here](https://help.autodesk.com/view/OARX/2024/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_DatabaseServices_Database_Wblock) and [here](https://help.autodesk.com/view/OARX/2024/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_DatabaseServices_Database)
