Skip to content

feat: Added support to delete builds on rententionDays#1922

Merged
saikrishna321 merged 2 commits intomainfrom
build-cleanup
Oct 29, 2025
Merged

feat: Added support to delete builds on rententionDays#1922
saikrishna321 merged 2 commits intomainfrom
build-cleanup

Conversation

@saikrishna321
Copy link
Member

Builds Cleanup Feature

Overview

This PR introduces a cleanup feature that allows users to permanently delete old builds and their associated data (sessions, session logs, and test event journals) based on a configurable retention period.

Features

UI Components

  • Cleanup Modal: A modal dialog accessible from the Builds page that allows users to specify a retention period (in days)
    • Default retention period: 30 days
    • Input validation for numeric values
    • Confirmation step before deletion
    • Styled with proper z-index to overlay sticky headers

Backend Implementation

  • New API Endpoint: POST /device-farm/api/dashboard/cleanup
    • Accepts retentionDays in request body
    • Returns detailed deletion counts (builds, sessions, session logs, test event journals)
    • Comprehensive error handling and logging

Technical Implementation

Date Calculation Logic

The cleanup uses precise date calculations to ensure consistent behavior:

// Cutoff date is calculated as start of day (midnight) for the retention date
const retentionDate = new Date();
retentionDate.setDate(retentionDate.getDate() - retentionDays);
retentionDate.setHours(0, 0, 0, 0); // Set to start of day (00:00:00.000)

Key Points:

  • Cutoff is set to midnight of the retention date, not the exact current time
  • Uses strict less than (lt) comparison to retain builds created on the cutoff day
  • Example: If retentionDays=2 on Oct 29:
    • Cutoff: Oct 27 00:00:00
    • Deletes: Builds created before Oct 27 (Oct 26 and earlier)
    • Keeps: Builds from Oct 27, Oct 28, and Oct 29

Data Deletion Process

Deletion follows proper order to respect foreign key constraints:

  1. Identify builds to delete - Finds builds with createdAt < retentionDate
  2. Fetch related sessions - Gets all session IDs for the builds
  3. Transaction-based deletion (atomic operation):
    • Delete session logs first (required due to ON DELETE RESTRICT constraint)
    • Delete test event journals
    • Delete sessions
    • Delete builds

All deletions happen within a database transaction to maintain data consistency.

Logging & Debugging

Comprehensive logging includes:

  • Total builds in database
  • Sample build dates for comparison
  • Exact cutoff date and timestamp
  • Count of builds found for deletion
  • Detailed deletion counts per entity type
  • Age calculation when no builds are found

Testing

Sample Data Script

Added src/scripts/inject-sample-data.ts to generate test data with various age ranges:

  • Builds from today
  • Builds from yesterday
  • Builds exactly 2 days old (boundary testing)
  • Builds 5-7 days old (recent)
  • Builds 35+ days old (old)

Usage:

npm run db:inject-sample-data
# or with custom options:
npm run db:inject-sample-data -- --today-builds 2 --yesterday-builds 3 --two-day-old-builds 5

Testing Scenarios

  1. Boundary Testing: Test with retentionDays=2 to verify 2-day-old builds are retained
  2. No Data: Verify graceful handling when no builds match criteria
  3. All Data: Test with retentionDays=0 to delete all builds
  4. Recent Data: Verify recent builds are not deleted with appropriate retention period

Files Changed

Frontend

  • dashboard-frontend/src/components/cleanup-modal/cleanup-modal.tsx - Modal component
  • dashboard-frontend/src/pages/Builds/index.tsx - Integration and cleanup handler
  • dashboard-frontend/src/api-service/index.ts - API service method

Backend

  • src/modules/dashboard/router.ts - Cleanup endpoint implementation
  • src/scripts/inject-sample-data.ts - Sample data generation script
  • package.json - Added npm script db:inject-sample-data

API Response Format

Success Response:

{
  "message": "Cleanup completed",
  "deletedBuilds": 3,
  "deletedSessions": 6,
  "deletedSessionLogs": 18,
  "deletedTestEventJournals": 6
}

No Builds to Delete:

{
  "message": "No builds older than 30 days found. All 5 build(s) are within the retention period.",
  "deletedBuilds": 0,
  "deletedSessions": 0,
  "deletedSessionLogs": 0,
  "deletedTestEventJournals": 0
}

Error Handling

  • Validates retentionDays parameter (must be number >= 0)
  • Returns 400 status for invalid input
  • Comprehensive try-catch with detailed error logging
  • Graceful handling of edge cases (empty database, no matches)

UI Improvements

  • Fixed modal z-index to properly overlay sticky headers
  • Improved modal centering with flexbox
  • Enhanced input styling for better visibility
  • Better error messaging in responses

Future Enhancements

  • Add warning for large deletion counts
  • Add undo functionality (optional)
  • Add scheduling/cron job support for automatic cleanup
  • Add email notifications for cleanup completion

saikrishna321 and others added 2 commits October 29, 2025 22:13
Co-authored-by: SrinivasanTarget <srinivasan.sekar1990@gmail.com>
Co-authored-by: SrinivasanTarget <srinivasan.sekar1990@gmail.com>
@saikrishna321 saikrishna321 merged commit a986350 into main Oct 29, 2025
5 checks passed
saikrishna321 pushed a commit that referenced this pull request Oct 29, 2025
## [11.2.0](v11.1.5...v11.2.0) (2025-10-29)

### Features

* Added support to delete builds on rententionDays ([#1922](#1922)) ([a986350](a986350))
@github-actions
Copy link

🎉 This PR is included in version 11.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@saikrishna321 saikrishna321 deleted the build-cleanup branch December 12, 2025 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant