Clean up after arcpy.StageService

07-11-2017 09:17 AM
New Contributor II

I am using a modified version of script # 3 from the help docs here to update several services each day. Data is copied to the server for this. My modification is only that I loop through several MXD files, so basically just a for loop wrapper around the script at the end of the post here.

The issue is that the arcpy.StageService creates a .sd file as well as folders containing a copy of the data it will upload to the server. As far as I can tell, there is no provision to have this tool clean up after itself after uploading the service.It is trivial to delete the SD file, but the copy of the data I am not sure about. It is placed in folders 0, 1, 2, ...This does not seem to be well documented.

Is there something I am missing? Does anyone know of a reliable way to clean up after this tool, so I don't accumulate copies of my data over time?

# Name:
# Description: Creates a service definition that can be used to overwrite an 
#              existing service. When this service definition is published it 
#              will overwrite the existing service.
# Requirements: Connection to an ArcGIS Server or My Hosted Services

# Import system modules
import arcpy
from arcpy import env
import xml.dom.minidom as DOM 

# Set environment settings
env.workspace = "C:/data"

# Set local variables
inServiceDefinitionDraft = "myMapService.sddraft"
outServiceDefinition = ""
newType = 'esriServiceDefinitionType_Replacement'

xml = draftPath + in_sd_draft
doc = DOM.parse(xml)
descriptions = doc.getElementsByTagName('Type')
for desc in descriptions:
    if desc.parentNode.tagName == 'SVCManifest':
        if desc.hasChildNodes():
   = newType
outXml = xml    
f = open(outXml, 'w')     
doc.writexml( f )     

# Execute StageService
arcpy.StageService_server(inServiceDefinitionDraft, outServiceDefinition)

# Set local variables
inSdFile = outServiceDefinition
inServer = "GIS Servers/myServerConnection"

# Execute UploadServiceDefinition
arcpy.UploadServiceDefinition_server(inSdFile, inServer)
0 Kudos
0 Replies