Challenge

Problem

Application logs accumulate relentlessly, consuming disk space and eventually crashing servers. This drill teaches you to build a maintenance script that identifies and deletes files older than a specified number of days. You'll learn time arithmetic, file metadata handling, and summary reportingโ€”essential skills for system administration tasks.

Difficulty: Beginner

Instructions

  1. Read the metadata.json file which contains log filenames, their ages in days, and sizes in bytes
  2. Find all files older than the specified days threshold
  3. Delete files that exceed the age threshold
  4. Output format: 'Deleted: filename.log (X days old)' for each deleted file
  5. Output summary: 'Total: X files deleted, Y.YY MB freed'
  6. Sort deleted files by name alphabetically

Files

Editable
Read-only

Hints

Hint 1

Read the metadata.json file using File.read and parse with JSON.parse

Hint 2

Iterate through the 'files' array in the metadata

Hint 3

Compare each file's age_days against the threshold

Hint 4

Use File.delete(path) to remove files

Hint 5

Track total bytes freed and convert to MB by dividing by 1048576.0

Hint 6

Use sprintf('%.2f', number) to format MB to 2 decimal places

Ruby 3.4

Provided Files (Read-only)

1. Deletes files older than threshold

Input:
delete_old_files('logs', 30)
Expected Output:
Deleted: app_old.log (45 days old)
Deleted: error_old.log (60 days old)
Total: 2 files deleted, 0.00 MB freed

2. Keeps recent files

Input:
delete_old_files('logs', 30)
puts '---'
puts File.exist?('logs/access_recent.log')
puts File.exist?('logs/debug_recent.log')
Expected Output:
Deleted: app_old.log (45 days old)
Deleted: error_old.log (60 days old)
Total: 2 files deleted, 0.00 MB freed
---
true
true
+ 2 hidden test cases