Daniel Baucom (Customer) asked a question.

Process to clean up old, archived files.

With all of our MOVEit Automation processes, after moving the files, we make a copy of the files that were moved and save them into an archive location. Initially we created an advanced task for each real task that was called by on completion. The cleanup task looped over that tasks archive folder to clean up files older than the time were needed to keep them.

 

This created issues with MOVEit Automation as it the load step could potentially load 10 of thousands of files to loop over to see if they needed to be removed yet. Each file was then logged to the database. This caused slow load times for the dashboard and caused the database to be artificially large. It also littered our tasks with a bunch of "cleanup" tasks.

 

We solved this problem by creating a powershell script to do the file cleanup and having a single global cleanup task that could be called with parameters to control it. The script takes 3 parameters, the path to cleanup (cleanup_path), how many days to keep (cleanup_days), and the file pattern to delete (cleanup_filter).

 

The script:

  1. try {
  2. # parameter for path to delete files
  3. $cleanup_path = $miaclient.MIGetTaskParam("cleanup_path")
  4. if ($cleanup_path -eq "") {
  5. $miaclient.MISetTaskParam("cleanup_path", "")
  6. throw "No path provided"
  7. }
  8.  
  9. try {
  10. #parameter for the number of days to keep files
  11. $cleanup_days = $miaclient.MIGetTaskParam("cleanup_days") -as [int]
  12. }
  13. catch {
  14. throw "Days is not a valid integer"
  15. }
  16. if ($cleanup_days -eq "" -or $null -eq $cleanup_days) {
  17. throw "No days provided"
  18. }
  19.  
  20. # parameter for the file match pattern, not required, will default to *.*
  21. # if not included.
  22. $cleanup_filter = $miaclient.MIGetTaskParam("cleanup_filter")
  23. if ($cleanup_filter -eq "") {
  24. $cleanup_filter = "*.*"
  25. }
  26.  
  27. #Delete files from path
  28. $miaclient.MILogMsg("Executing cleanup job")
  29. $FilesToDelete = Get-ChildItem -Path $cleanup_path -Filter $cleanup_filter -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays((-1 * $cleanup_days)) }
  30.  
  31. # Delete each file
  32. $FilesToDelete | ForEach-Object {
  33. $miaclient.MILogMsg("Deleting file: $($_.FullName)")
  34. Remove-Item $_.FullName -Force
  35. }
  36.  
  37. $miaclient.MILogMsg("Deleted $($FilesToDelete.Count) files older than $cleanup_date in $cleanup_path.")
  38. }
  39. catch {
  40. $miaclient.MILogMsg("Caught exception: $Error")
  41. $miaclient.MILogMsg($Error[0].ScriptStackTrace)
  42. # 10000 is a suggested error code for errors in user scripts.
  43. $miaclient.MISetErrorCode(10000)
  44. $miaclient.MISetErrorDescription("Error: " + $Error)
  45. }

Task containing the script is specifically setup as a On Success Run task:

imageThe global task is a very simple advanced task that just calls the cleanup script, and gets the parameters from the parent tasks call to run.

image 

 

We thought this may be useful to others, so I am sharing it here.


Rick Willis and Jay Goodell like this.
  • Samik Chakraborty (Progress Software Development)

    Hi Daniel,

     

    Thanks for the advice.

     

    Want to make a point as it seems that the used case actually points towards that one single Drive or folder that is used as archive and the destination is local.

     

    So instead of a PS file, we may write a simple BAT script and target that folder on D Drive and delete files older than 30 days. Simple BAT file and can be triggered using the Windows task scheduler.

     

    Let me know what you think.

     

    @echo off

    setlocal

     

    #Set the target folder path

    set "ARCHIVE_FOLDER=C:\Path\To\Your\Folder"

     

    #Set the number of days

    set "DAYS=30"

     

    #Delete files older than 30 days

    forfiles /p "%ARCHIVE_FOLDER%" /s /m *.* /d -%DAYS% /c "cmd /c del @path"

     

    endlocal

    • Daniel Baucom (Customer)

      We chose not to go the batch script/windows scheduled task route, because we have many different automations in MOVEit where we keep files in the archive for varying lengths. Having this script in MOVEit that can be called with parameters passed in, allows us to run it after the automation finishes and cleans up just those files for that automation that are older than we want to keep. It doesn't point to a singular directory; it points to the archive directory that the automation passes in via the parameter.

       

      I was just sharing what we did so that if others found it useful they could use it as well.

  • Charles Lubiewski (First Bank)

    We still have an older Ipswitch, Inc VBScript imported into our instance named "CleanupFolder.vbs". With the recent announcement that VBScript is being deprecated by Microsoft, having a PowerShell version of this kind of code is a good thing! Thank you for sharing!

  • Samik Chakraborty (Progress Software Development)

    @Charles Lubiewski (First Bank)​  We have not deprecated the use of VBscript in the Automation application yet.

    We are aware of the changes and working on them.

    We would be striving to change the scripts to supported code but cannot change or alter the custom scripts as those are managed by BU internally and not by OEM.

    I was not able to find this specific script in MIA version 17.1 or 17.0 where the Admin console has been deprecated.