How To Delete Old Files From Your FTP Server | JSCAPE
If you need to clear some space on your FTP server, you can use triggers to automatically delete old files. Check out our illustrated tutorial online!
Overview
Here's another task many customers want to automate on their SFTP, FTPS, or FTP server. They want their server to monitor certain directories and delete old files found there as soon as the files have reached a certain age. In this tutorial, we're going to show you how to automatically delete old files from JSCAPE MFT Server using triggers. These instructions apply irrespective of the file transfer protocol you activate on the server, so if you think this is something you can use, feel free to read along.
What we're trying to do
Ok. So this is what we'd like to achieve in this automated file transfer tutorial. Every night, at 9:30 PM, our MFT server should scan the contents of a particular directory and look for files that have reached a year old. You can specify any age but, for this example, let's just use 365 days or 1 year. If any such files are found, the server should then automatically delete those files.
So, for example, if today is March 4, 2015, these three files would be deleted.
To achieve this, we would need:
- A directory monitor that would scan a specific directory for one year old files;
- A trigger that would activate the directory monitor every night at 9:30 PM; and
- Another trigger that would automatically delete each one year old file found by the directory monitor.
Now that we have a plan, let's put that plan into action.
1. Create a directory monitor to detect old files
Go to the Directory Monitors node and create a new directory monitor. Give the monitor a name (e.g. "Year Old Files"). After that, click the Browse button and then select the directory whose files you want to monitor.
Make sure you uncheck the Monitor interval checkbox. The reason we want to do this is because it's going to be much easier to schedule the time for the monitor to go into the directory and look for old files if we use a time-based trigger instead of just relying on the Monitor interval.
After unchecking the Monitor interval, go to the EVENTS panel and check the File exceeds age of check box. You can then specify the age of the files you want to delete. Since we want to delete one year old files in this example, just enter 365.
As soon as you're done, click the OK button.
Once you've created the directory monitor for detecting old files, the next step would be to create a trigger that would activate that directory monitor every night at 9:30 PM.
2. Create a trigger that would activate the directory monitor every night
Go to the Triggers node and add a new trigger. Give the trigger a name (e.g. "Activate Year Old Files Monitor") and select the Current Time from the Event type drop-down list. Click the Next button to proceed.
Specify the time at which this trigger should execute. Click Next to proceed.
Because this trigger is supposed to activate the directory monitor we created earlier, expand the drop-down list box and then select the Run Directory Monitor trigger action. Click OK.
Select the name of the directory monitor you created earlier (e.g. "Year Old Files"). Click OK to proceed.
We are now ready to proceed to the last step.
3. Create a trigger that would run each time an aged file is detected and then subsequently delete that particular file.
Add another trigger and give it an appropriate name (e.g. "Nightly File Deletes"). Expand the Event type drop down list and select Directory Monitor File Aged. Click Next to continue.
Enter this expression into the Expression box:
Monitorname = "Year Old Files"
This expression will tell the trigger to perform the subsequent trigger action if and only if the Directory Monitor File Aged event was fired by the "Year Old Files" monitor. Click Next to proceed.
Select the Delete File trigger action and then click the OK button. It is this trigger action that will delete the old file that's detected by the directory monitor.
Whenever the Directory Monitor File Aged event fires, it will consequently return the complete path of the aged file that was detected. This path will be stored in the variable named %File%. Thus, we can use this variable to specify which file should be deleted. Note that the value of this variable changes each time the Directory Monitor File Aged is fired (i.e., each time an aged file is detected).
Click OK to proceed.
Summary
Let us now summarize the series of events we expect to take place:
1. As soon as the clock strikes 9:30 PM, the Activate Year Old Files Monitor trigger will activate the Year Old Files directory monitor.
2. The directory monitor will dive into the directory it's assigned to monitor and scan for 1 year old files.
3. If a 1 year old file is found, the Directory Monitor File Aged event will then fire.
4. This event will subsequently cause the Nightly File Deletes trigger action to execute.
5. The trigger checks to see whether the monitor that fired the Directory Monitor File Aged is the Year Old Files monitor. If it is, the trigger proceeds to execute its Delete File trigger action.
6. The trigger action executes, using the value of the %File% variable to determine which file it should delete. The file in question is deleted.
7. The Year Old Files directory monitor continues scanning the directory for more 1 year old files. If another one is found, the process is repeated starting at step #3.
That's it. Now you know how to automatically delete old files from your managed file transfer server.
Get Started
Would you like to try the steps in this tutorial? Download the free, fully-functional evaluation edition of JSCAPE MFT Server now.
Recommended reads
You might also find these posts interesting: