Deleting old load test results

Load testers sometimes need to clean up old load test results from the VSTS load test results store. The following SQL script deletes all load test results older than two weeks (though the timeframe is easily changed).

If you have a lot of results, this could take a while to run. I would recommend that you do not run this script while a load test is running as we have seen this cause the load test to timeout while trying to write to the database.

-- This script deletes all load test run older than two weeks
-- To change the timeframe, change the number of days from 14 to the desired number

USE LoadTest
DECLARE @LoadTestRunId int
DECLARE OldLoadTestsCursor CURSOR FOR
    SELECT LoadTestRunId FROM LoadTestRun WHERE datediff(dd, StartTime, getdate()) > 14

OPEN OldLoadTestsCursor
FETCH NEXT FROM OldLoadTestsCursor INTO @LoadTestRunId

WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC Prc_DeleteLoadTestRun @LoadTestRunId
    FETCH NEXT FROM OldLoadTestsCursor INTO @LoadTestRunId
END

CLOSE OldLoadTestsCursor
DEALLOCATE OldLoadTestsCursor