Delete files older than x days or after x amount of time in PHP

Today we’ll show you how to delete files older than x days or after x amount of time in PHP. Sometimes we need to delete files older than n number of days from a specific directory.

You can also use the following article to delete all files and subfolders from the directory.

In this article, we will show you a simple way to remove files older than specified days or time from the directory using PHP.

Delete files from directory

  1. Older than x days
  2. Older than x amount of time
  3. Delete files and subfolders older than x amount of time

1. Older than x days

Here, we will create a function where we will pass two parameters, first one is directory/folder path and second parameter is number of days.

This script deletes files from specified folder only, not from child folders.

2. Older than x amount of time

Now, we will write a script to delete files that are x amount of time old.

3. Delete files and subfolders older than x amount of time

If you want to delete files and subfolders based on the given time then you can use the following code. (Thanks, Amir for sharing your knowledge with us.)

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *