Select to view content in your preferred language

arcpy check to see if version exists

4394
3
Jump to solution
06-15-2015 07:27 AM
MattFrancis1
New Contributor

Hey All,

I am having a hard time finding a method to check if a version exists.  If it exists, I would like to delete it and recreate it

Currently my script uses an inelegant try: except: block, because I don't know how to check to see if a version named "iLOVEtacos" exists.  I Instead of expecting a failure condition and dealing with the fallout, I would rather have if: else: statements that look more like this. Can this be accomplished?

import arcpy

import os

ws = os.path.join(os.environ['APPDATA'],"ESRI","Desktop10.2","ArcCatalog", "cfile" + ".sde")

vname = "iLOVEtacos"

vowner = "SDE"

if arcpy.FindVersion(ws, vowner, vname):

    arcpy.DeleteVersion_management(ws, vname)

arcpy.CreateVersion_management(ws, "SDE.DEFAULT", vname, "PUBLIC")

Thanks,

Matt

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Matt,

You could accomplish this with something similar below:

import arcpy
from arcpy import env
env.workspace = r"Database Connections\SQLSERVER.sde"

for version in arcpy.da.ListVersions(env.workspace):
    if 'iLOVEtacos' in version.name:        
        arcpy.DeleteVersion_management(env.workspace, version.name)

View solution in original post

3 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Matt,

You could accomplish this with something similar below:

import arcpy
from arcpy import env
env.workspace = r"Database Connections\SQLSERVER.sde"

for version in arcpy.da.ListVersions(env.workspace):
    if 'iLOVEtacos' in version.name:        
        arcpy.DeleteVersion_management(env.workspace, version.name)
MattFrancis1
New Contributor

That's a thing of beauty.  Thanks for your help!

Matt

0 Kudos
SepheFox
Frequent Contributor

I'm glad you found a solution, Matt. Do you mind marking the question as answered? Thanks!

0 Kudos