delete()
public function delete(string $file_path): bool
Description
Deletes a file from the filesystem. Throws an exception if the file does not exist, cannot be deleted, or is in a restricted path.
Parameters
| Parameter | Type | Description |
| $file_path |
string |
The path to the file to be deleted. |
Return Value
| Type | Description |
| bool |
Returns true if the file is successfully deleted. |
Example #1
The code sample below demonstrates the most basic file deletion operation.
$file_path = 'modules/uploads/old_document.pdf';
// Check if file exists before attempting deletion
if ($this->file->exists($file_path)) {
$result = $this->file->delete($file_path);
if ($result === true) {
echo 'File deleted successfully';
}
} else {
echo 'File not found';
}
Check Before You Act: The delete() method throws an exception if the file doesn't exist or cannot be deleted. It's recommended to always check if a file exists using $this->file->exists($file_path) before attempting deletion. This allows you to handle missing files gracefully with redirects or user-friendly messages, rather than relying on exception handling for expected conditions.
Example #2
The example above shows how to delete a user's uploaded resume file when they request its removal.
public function delete_resume(): void {
$user_id = segment(3, 'int');
// Get the user's current resume filename from the database
$user_obj = $this->db->get_where($user_id, 'users');
if ($user_obj === false) {
redirect('users/not_found');
}
$resume_filename = $user_obj->resume_file; // e.g., 'x7f9d2e1b4a.pdf'
// Build the full path to the file
$file_path = 'modules/users/resumes/' . $resume_filename;
// Check if file exists before attempting deletion
if ($this->file->exists($file_path)) {
$result = $this->file->delete($file_path);
// Update database to remove the file reference
$data['resume_file'] = null;
$this->model->update($user_id, $data, 'users');
set_flashdata('Resume deleted successfully');
redirect('users/profile/' . $user_id);
} else {
redirect('users/not_found');
}
}
Example #3
The example above demonstrates how to delete temporary files after processing is complete.
public function process_import(): void {
$import_id = segment(3, 'int');
// Process an uploaded CSV file
$uploaded_file = 'temp/imports/data_' . $import_id . '.csv';
// Check the file exists before processing
if ($this->file->exists($uploaded_file)) {
// Read and process the CSV
$csv_data = $this->file->read($uploaded_file);
// ... process the data ...
// Clean up the temporary file after successful processing
$result = $this->file->delete($uploaded_file);
set_flashdata('Import completed and temporary file cleaned up');
redirect('imports/success');
} else {
redirect('imports/not_found');
}
}
Example #4
The example above shows how to delete old backup files as part of a maintenance routine.
public function cleanup_old_backups(): void {
$backup_dir = 'backups/daily';
if (!$this->file->exists($backup_dir)) {
redirect('backups/setup');
}
// List all items in backup directory
$items = $this->file->list_directory($backup_dir);
// Delete backups older than 30 days
$cutoff_date = strtotime('-30 days');
$deleted_count = 0;
foreach ($items as $item) {
$file_path = $backup_dir . '/' . $item;
// Skip directories
if (is_dir($file_path)) {
continue;
}
if ($this->file->exists($file_path)) {
$file_date = filemtime($file_path);
if ($file_date < $cutoff_date) {
$this->file->delete($file_path);
$deleted_count++;
}
}
}
set_flashdata("Deleted {$deleted_count} old backup files");
redirect('backups/manage');
}
Database Consistency: When deleting files that are referenced in your database, always update or remove the database record after a successful deletion. Failing to do this will result in broken file references and "file not found" errors when users try to access files that no longer exist on the filesystem.