Why 'del mxd'

5311
4
05-12-2011 11:55 AM
AnthonyFarndon
Occasional Contributor
Hi

could anyone advise on the significance/requirement to delete the mxd variable after use, all example code ends with 'del mxd'?

I have some arcpy.mapping code wrapped up in a class, so wonder why, if at all, I need to do this. Should I call this in the class __del__ deconstructor perhaps, call del on the class/object itself after use or add in a custom deconstructor function after I have finished with the object to call del mxd on all the mxd's used in the class?

Thanks
Tags (2)
4 Replies
DanPatterson_Retired
MVP Emeritus
It just deletes the reference to the *.mxd...called clean-up.  Not necessary unless you are running many processes, but is generally good practice (think teenagers and their rooms ... clean-up is good)
0 Kudos
JakeSkinner
Esri Esteemed Contributor
The 'mxd' variable will place a lock on the MXD.  Without deleting this variable, the lock will remain. 

For example, you run a python script that makes some change to an MXD and you don't delete the 'mxd' variable.  Then you open this MXD in ArcMap, make another change and then try saving.  You will receive an error that it is in use.
AnthonyFarndon
Occasional Contributor
thank you, makes sense now
0 Kudos
MikeMacRae
Occasional Contributor III
This also clears space in memory, which helps if your program is memory intensive.
0 Kudos