Online Tutorials & Training Materials | STechies.com
Register Login

CronJob to delete files

|| 0

CronJob to delete files
Stechies

How to use CronJob to Delete Files?

Step 1) Please try following groovy_script in your HAC -> Console -> Scripting Languages to make sure the scripts works.

import org.apache.log4j.Logger

LOG = Logger.getLogger("Delete-file-script")

// Files Older than 30 days will be deleted
def MAX_LOG_FILE_AGE = 30 * 24 * 60 * 60 * 1000
def deleteDate = System.currentTimeMillis() - MAX_LOG_FILE_AGE

//set the folders
def logFileDirs = ["C:tmp","C:tmp1", "C:tmp2" ]
def logsFiles = []

logFileDirs.each { logDir ->
// build list of files
new File(logDir).eachFileRecurse { file ->
if (file.isFile() && (file.lastModified() < deleteDate)) {
logs << file
}
}
try {
logs.each { victim ->
LOG.info "deleting $victim.path"
victim.delete()
}
} catch (e) {
LOG.error("Error in Deleting:", e)
}
}

Step 2) Navigate to backoffice -> Scripting -> Create a new GROOVY script. Then please copy the content of verified groovy script to script content.

Step 3) Navigate to backoffice -> ScriptingJobs -> Create a job. The ScriptURI format is model://<Script Name created in step 1>

Step 4) Navigate to backoffice -> CronJobs -> Create a new cronjob with the job created in step 3.

Here please set the time Schedule based on your request.