Skip to content

Instantly share code, notes, and snippets.

@narendrans
Created April 17, 2014 15:23
Show Gist options
  • Save narendrans/10991715 to your computer and use it in GitHub Desktop.
Save narendrans/10991715 to your computer and use it in GitHub Desktop.
'''
@Author: Naren
@Date: 4/16/2014
This script can be used to delete uploads longer than specified period in years for a wordpress multisite.
Note: Because uploads or wordpress blogs itself can be migrated, the timestamp of an upload wont necessarily reflect the date of creation.
Further the uploads are organized in such a way as below
year
month
date
Hence this script!
Algorithm:
Get current and previous year
Set the path to the uploads directory of wordpress: /wp-content/blogs.dir
Walk through the directory to find all subdirectories and put it in a list
Remove all entries that contains previous and current year and put it in final list
rm -rf all dirs in the final list
'''
import os, sys
from datetime import date
def main():
if len(sys.argv) < 2:
print 'Usage: ' + sys.argv[0] + ' <number of years>'
print 'ex: ' + sys.argv[0] + ' 2 <-- To delete files older than 2 years'
print 'ex: ' + sys.argv[0] + ' 3 <-- To delete files older than 3 years'
return
years = int(sys.argv[1])
remove = []
for i in range(0,years):
remove.append('files/' + str(date.today().year - i))
path = '.' # run the script in the blogs.dir directory itself
# Directory walk to get all directories in the uploads folder
def iterdirs(path):
for root, dirs, files in os.walk(path, topdown=False):
for d in dirs:
yield os.path.join(root, d)
files = []
for d in iterdirs(path):
if 'files' in d:
files.append(d)
newlist = []
newlist = [f for f in files if not any(r in f for r in remove)]
directoryListToRemove = []
for a in newlist:
mystr = a[-4:]
if mystr.isdigit():
directoryListToRemove.append(a)
size = ''
for a in directoryListToRemove:
size = size + ' ' + a + ' '
size = size.strip()
if not directoryListToRemove:
print 'No uploads older than ' + sys.argv[1] + ' years is found'
return
os.system('du -csh ' + size + ' | sort -h')
option = raw_input('Above is the list of folders going to be removed, Do you wish to continue? [y|n]: ')
if 'y' in option:
for a in directoryListToRemove:
sys.stdout.write('\nExecuting ' + 'rm -rf ' + a)
os.system('rm -rf ' + a)
print '\nAll commands executed successfully'
else:
print 'Folders are not deleted'
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment