I am using ArcGIS Desktop 10.0 and Python 2.6.6 and am trying to make an update script to copy the geodatabase to a backup folder with the date as part of the name, got that to work.

Then export features from my editing geodatabase to the one on our server, got that working.

But now I want to, at the end of my script, to go back to the backup folder and delete any geodatabases there that are older than 1 week to keep it clean.

Tried a number of ways but they didn't work, any suggestions?

import re
import os
import datetime
import time
import sys

from subprocess import call

now = time.time()
cutoff = now - (7 * 86400)

files = os.listdir("C:\GIS Info\Test\Backup")
for xfile in files:
        if os.path.isfile( "C:\GIS Info\Test\Backup" + xfile ):
                t = os.stat( "C:\GIS Info\Test\Backup" + xfile )
                c = t.st_ctime

                # delete file if older than a week
                if c < cutoff:
                        os.remove("C:\GIS Info\Test\Backup" + xfile)

This is the code I was trying to use that got me the furthest.

  • Are you wanting the update script to be in python, or would shell command work as well?
    – evv_gis
    Commented Nov 5, 2014 at 20:02
  • 2
    What do you mean by "it didn't work"? Were you unable to delete or unable to filter to the correct geodatabases?
    – Beck
    Commented Nov 5, 2014 at 20:10
  • for delete: arcpy.Delete_management(full_path), for last access time see stackoverflow.com/questions/16464228/… Commented Nov 5, 2014 at 21:31
  • Can you include a code snippet of how you are trying to "go back to the backup folder and delete any geodatabases there that are older than 1 week", please? No need to show us all the ways that you have tried, just the one that you thought should have been the most likely to work. Also include any error messages thrown by running that code snippet. Also, what type(s) of geodatabases are you trying to delete?
    – PolyGeo
    Commented Nov 5, 2014 at 23:01
  • 2
    how are you determining that GDBs are older than one week? Are you using the file system time, as @MichaelMiles-Stimson is suggesting, or using the GDB name itself (you mention that the date is part of the file name)? Commented Nov 5, 2014 at 23:08

1 Answer 1


Since FGDB's are treated as folders, this script looks for folders in a directory older that seven days, then uses the Delete management tool to remove the old folders.

import os
import time
import arcpy

dirPath = r"C:\test_data"
cutoff =  7 * 86400
present = time.time()
directories = []
for root, dirs, files in os.walk(dirPath, topdown=False):
    for name in dirs:
        subDirPath = os.path.join(root, name)
        if (present - os.path.getmtime(subDirPath)) > cutoff:

for gdb in directories:

It doesn't work only on GDB's, it deletes all sub directories older that seven days. But you could tweak it to only delete GDB's and leave other directories in place.

  • That's got it, thanks. Don't think there will be an issue with other directories Backup should only hold the backedup databases.
    – MrKingsley
    Commented Nov 6, 2014 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.