Automatically Delete Files in File Manager
Overview
This article explains how to automatically delete files in the file manager using a file manipulation Smarty plugin.
In this example, we will implement the following batch process:
- Get a list of files in a specific directory
- Delete files that have not been updated in over a week
- Output a log of the deleted files in CSV format and place it in the file manager
What you will learn
You will learn how to perform the following file operations:
- Get file information in a specific directory
- Delete files
- Add files
- Write values to a text file
Prerequisites
- Basic understanding of Smarty v2 syntax
- Familiarity with how to use batch processing
Prepare the target directory
Create the following directory structure in the file manager:
files/ltd
`-- file_plugins_tutorial
|-- assets # Directory to place files for deletion
`-- logs # Directory to place deletion log files (CSV)
Implement the file deletion process
Create a new batch process
Create a new batch process.
First, we will implement the file deletion process.
Click [Operation] -> [Batch Process].
Click [Add].
The batch process editing screen will open. Set the following:
Field | Value |
---|---|
Title | file_plugins_tutorial |
Identifier | file_plugins_tutorial |
Type | Every day at 02:00 |
Process | Code below |
{* Set the timestamp for comparison *}
{assign var='timestamp_to_compare' value='-1 week'|strtotime}
{* Read each file in the directory *}
{read_dir name='files_user' path='/files/ltd/file_plugins_tutorial/assets' file_var='file_info' type='file' recursive=true}
{* Get the file's update timestamp *}
{assign var='timestamp_updated_at' value=$file_info.mtime}
{* Compare the timestamps *}
{if $timestamp_updated_at < $timestamp_to_compare}
{* If the update timestamp is over a week ago, delete the file *}
{remove_file status_var='status' path=$file_info.path}
{/if}
{/read_dir}
Once the settings are done, click [Add] to add the batch process.
For more details on the parameters of the read_dir plugin, refer to the Smarty plugin documentation.
Test the file deletion process
Once the batch process is set up, test its functionality.
Click [Run now] on the batch process editing screen.
The batch process will run and delete files that have not been updated in over a week.
If you want to test it immediately, change the '-1 week'
part to '-1 hour'
or 'now'
, and test it.
{assign var='timestamp_to_compare' value='-1 week'|strtotime}
Adding log file output processing
Next, we will add a description to save the log file when a file is deleted.
Updating the batch processing
Click on the title on the batch list page to open the batch processing that was added earlier.
Add the log file output processing to the processing. The overall processing will be as follows:
{* Create the header row of the log file *}
{assign var=header_columns value=$empty_array}
{append var=header_columns index=path value="Path"}
{append var=header_columns index=date value="Deleted date"}
{* Add the header row to the log file (temporary area) *}
{write_file var=path value=$header_columns}
{write_file path=$path value="\n" is_append=1}
{* Set the timestamp to compare *}
{assign var='timestamp_to_compare' value='-1 week'|strtotime}
{* Set the current date and time *}
{date var='now' time='now' format='Y-m-d H:i:s'}
{* Read each file in the directory one by one *}
{read_dir name='files_user' path='/files/ltd/file_plugins_tutorial/assets' file_var='file_info' type='file' recursive=true}
{* Get the update date and time (timestamp) of the file *}
{assign var='timestamp_updated_at' value=$file_info.mtime}
{* Compare the timestamps *}
{if $timestamp_updated_at < $timestamp_to_compare}
{* If the update date and time is more than a week ago, delete the file *}
{remove_file status_var='status' path=$file_info.path}
{* Add the information of the deleted file to the log file (temporary area) *}
{assign var=row value=$empty_array}
{append var=row index=path value=$file_info.path}
{append var=row index=date value=$now}
{write_file path=$path value=$row is_append=1}
{write_file path=$path value="\n" is_append=1}
{assign var=make_log_file value=true}
{/if}
{/read_dir}
{* Write the log file (temporary area) to the file manager *}
{if $make_log_file}
{date var='file_name' time='now' format='Ymd_His'}
{put_file tmp_path=$path path="/files/ltd/file_plugins_tutorial/logs/log_`$file_name`.csv"}
{/if}
Once the settings are done, click on [Update] to apply the changes.
For more details on write_file, date, and put_file, please refer to the Smarty Plugin.
Testing the log file output processing
Click on [Run immediately] on the batch processing edit screen.
The batch processing will be executed, and files with a last update date more than a week ago will be deleted, leaving logs in the logs directory.
If you want to check immediately, change the '-1 week'
part to ' -1 hour'
or 'now'
, etc. and check.
{assign var='timestamp_to_compare' value='-1 week'|strtotime}
With this, the configuration and verification are complete. Please use it for file capacity management.
Related Documents
Support
If you have any other questions, please contact us or check out Our Slack Community.