Deleting all but the two newest files on the server

Hello,

There are prefixes in files and directories
example: adem-2023-10-05-14:23:15
Among the files prefixed with adem, I want to delete all but the newest two files.
How to do this?

Just a note - delete with caution

<?php

$dirPath = '/path/to/directory'; // Replace with your directory path

// Scan the directory for files with the prefix "adem"
$files = array_filter(scandir($dirPath), function($file) {
    return strpos($file, 'adem') === 0; // Check if the file starts with "adem"
});

// Sort the files based on their last modification time
usort($files, function($a, $b) use ($dirPath) {
    return filemtime($dirPath . '/' . $b) - filemtime($dirPath . '/' . $a);
});

// Keep the newest two files and delete the rest
while (count($files) > 2) {
    $fileToDelete = array_pop($files);
    unlink($dirPath . '/' . $fileToDelete);
}

echo "Cleanup complete.";

?>

1 Like

Thank you very much for the answer and example.
I’ll try it on Monday.
Does the while loop work like this?
I did not know that.

I tested it online from mobile, this example will work for me.
Thank you again

I used it in both areas, it works perfectly, thank you very much again

It is always necessary to take into account the recommendations of professionals :grinning_face_with_smiling_eyes: :grinning_face_with_smiling_eyes: :grinning_face_with_smiling_eyes:
I tried the code locally on the test page, everything was fine
Then I added the code to the actual page and transferred it to the remote server
I wanted to test it on the remote server, it gave me the error “directory could not be opened”
I removed the forward slash “/” at the end of the source directory
I tried again and it deleted and the page was refreshed but it gave a 404 file not found error. :grinning: :grinning: :grinning:
I looked at the website directory, there are a few files left, directories, files, everything has been deleted. :grinning: :grinning: :grinning: :grinning: :grinning:
I had previously taken a backup of the website, I uploaded again :grinning: :grinning: :grinning:

I found the error, I had previously applied unset($row) to database variable “$row”
Since I added the file deletion code under unset($row), the information could not be retrieved from the database and the prefix of the file could not be accessed.

Since there is a date in the directory and file, I tried to take precautions as follows.

function validateDate($date, $format = 'Y-m-d-H-i')
{
	$d = DateTime::createFromFormat($format, $date);
	return $d && $d->format($format) == $date;
}

while (count($folders) > 2) {
	$folderToDelete = array_pop($folders);
		$filedate = substr($folderToDelete, -16); // Directory name: "prefix-2023-10-11-09-14"
		if(validateDate($filedate)){
			folderDelete($path.$folderToDelete);
		}
}

It is necessary to do the same for the file, but the file has a prefix, date and file extension, so the date is in the middle.
How should it be done?
File name: frefix-2023-10-11-09-18-any text.sql or .sql.gz

substr($fileToDelete, strpos($fileToDelete, $prefix) + strlen($prefix), 16);
Sponsor our Newsletter | Privacy Policy | Terms of Service