Skip to main content

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:

  1. Get a list of files in a specific directory
  2. Delete files that have not been updated in over a week
  3. 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

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)

Image from Gyazo

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].

Image from Gyazo

Click [Add].

Image from Gyazo

The batch process editing screen will open. Set the following:

FieldValue
Titlefile_plugins_tutorial
Identifierfile_plugins_tutorial
TypeEvery day at 02:00
ProcessCode below
file_plugins_tutorial
{* 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}

Image from Gyazo

Once the settings are done, click [Add] to add the batch process.

info

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.

Image from Gyazo

The batch process will run and delete files that have not been updated in over a week.

tip

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.

Image from Gyazo

Add the log file output processing to the processing. The overall processing will be as follows:

file_plugins_tutorial
loading...

Once the settings are done, click on [Update] to apply the changes.

info

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.

Image from Gyazo

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.

Image from Gyazo

Image from Gyazo

tip

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.


Support

If you have any other questions, please contact us or check out Our Slack Community.