Povylas
Povylas

Reputation: 786

Automatically delete files from webserver

I need to delete files from a directory in my web-server and I need some script to do it automatic. And there is few more conditions which need to be fulfilled.

For deleting files I was thinking of some kind of corn task but I do not know a thing about them so it is tricky for me.
To delete files then a certain limit is reached I was thinking that some kind of PHP script my help. May be there is a library meant for these things.
Any way I will be very happy for any kind of help. May be I just need some fresh thoughts to come up with a right keyword for google ;P

EDIT: My server runs on linux.

Upvotes: 1

Views: 5358

Answers (5)

chetal
chetal

Reputation: 51

This can also be achieved using logrotate and systemd timer as shown in this how-to guide.

Sample logrotate config, say /path/logrotate/web-server-cleanup.conf:

/path/to/directory/* { 
    size 1G
}

Above config will remove all files that match /path/to/directory/* path regex and are at least 1G in size.

If you want it to run daily, you can configure systemd service that runs with systemd daily timer.

Sample systemd config /lib/systemd/system/web-server-cleanup.service:

[Unit]
Description=Rotate log files
Documentation=man:logrotate(8) man:logrotate.conf(5)
ConditionACPower=true

[Service]
Type=oneshot
ExecStart=/usr/sbin/logrotate /path/logrotate/web-server-cleanup.conf -s /path/logrotate/web-server-cleanup.conf.state

Sample systemd timer config /lib/systemd/system/web-server-cleanup.timer:

[Unit]
Description=Daily rotation of log files
Documentation=man:logrotate(8) man:logrotate.conf(5)

[Timer]
OnCalendar=*-*-* 00:00:00
AccuracySec=1h
Persistent=true

[Install]
WantedBy=timers.target

You can enable the timer using sudo systemctl enable --now web-server-cleanup.timer

Upvotes: 0

Frank
Frank

Reputation: 1

Go to your cPanel, select cron jobs and use the following command to create it:

find /home/your_account/public_html/temp/* -mmin +360 -exec rm -r {} \;

It will clean the "temp" (or any other name) file every 360 seconds. You can change that as well.

I am told that if you add the following to the end of the file it will prevent you from getting e-mails every time the cron job executes: /dev/null 2>&1

Upvotes: 0

John
John

Reputation: 11

Here is an easy way to automatically perform a file deletion depending on the file date : Webmastips : Tips and Tools for webmasters : Automatic File Deletion triggered by a specific date You can customize and automate this task in order to fit your needs. Regards,

Upvotes: 0

Adam Khoury
Adam Khoury

Reputation: 21

Here is a good cron job tutorial and explanation:

http://www.youtube.com/watch?v=lbJrk55Ae1c

Automate any script to execute. Just write a script that removes all files in folder and that is what you set your cron job to target.

And here is a tutorial about how a PHP programmer can access all files in a folder and use the php unlink() function on each in succession to delete the files. unlink($file);

http://www.developphp.com/view_lesson.php?v=239

Upvotes: 2

You
You

Reputation: 23824

If your server is running linux, a cron job is the best option. Simply write a script that checks the conditions you mention (using e.g. stat and some other tools) and deletes the file if they apply, and set crond to run it every 24 hours. Here's a short introduction to cron.

On Windows, you'd do something similar with a script and a scheduled task.

Upvotes: 4

Related Questions