Test Data Deletion Fix Script ATF

ServiceNow Use6
Tera Guru

Hi,

Has anyone used Test Data removal using the Fix Script in ATF? If yes, can you please share the script? At my company, I create dummy data for each test case, so I didn't need to use any table Cleanup or anything. Kindly help.

Regards

Suman P.

1 ACCEPTED SOLUTION

nishant3101
Giga Contributor

Hi @ServiceNow Use6 ,

 

Yes, you can remove ATF test data using a Fix Script. Below is a simple and safe example you can use. The recommended approach is to tag your test records during creation and then delete only those tagged records.

 

Example Fix Script (Recommended – delete by test tag):

(function runFixScript() {
    // Safety check – allows only Dev/UAT/Test instances
    var instance = gs.getProperty('instance_name') || '';
    if (!(/dev|test|uat|sub/i).test(instance)) {
        gs.error('Fix Script aborted: Instance check failed (' + instance + ')');
        return;
    }

    // CHANGE THIS to the tag you apply during ATF record creation
    var ATF_TAG = 'ATF_RUN_20251130';

    // Add all tables where your ATF creates test data
    var tables = [
        'incident',
        'u_my_custom_table'
    ];

    tables.forEach(function(tbl) {
        var gr = new GlideRecord(tbl);
        gr.addQuery('u_atf_tag', ATF_TAG);
        gr.query();

        var count = 0;
        while (gr.next()) {
            gr.deleteRecord();
            count++;
        }

        gs.info('Deleted ' + count + ' records from ' + tbl + ' with tag ' + ATF_TAG);
    });

})();

 

 

If you don’t have a tag field, you can also delete based on sys_created_by (e.g., your ATF service account), but this is less safe:

var gr = new GlideRecord('incident');
gr.addQuery('sys_created_by', 'atf_user'); 
gr.deleteMultiple();

 

Best practice:
When creating test data in ATF (via server-side script step), set a tag like:

gr.u_atf_tag = 'ATF_RUN_20251130';

 

This makes cleanup reliable and avoids accidental deletion of non-test records.

 

Hope this helps!
If it resolves your query, please mark the answer as correct.

View solution in original post

2 REPLIES 2

nishant3101
Giga Contributor

Hi @ServiceNow Use6 ,

 

Yes, you can remove ATF test data using a Fix Script. Below is a simple and safe example you can use. The recommended approach is to tag your test records during creation and then delete only those tagged records.

 

Example Fix Script (Recommended – delete by test tag):

(function runFixScript() {
    // Safety check – allows only Dev/UAT/Test instances
    var instance = gs.getProperty('instance_name') || '';
    if (!(/dev|test|uat|sub/i).test(instance)) {
        gs.error('Fix Script aborted: Instance check failed (' + instance + ')');
        return;
    }

    // CHANGE THIS to the tag you apply during ATF record creation
    var ATF_TAG = 'ATF_RUN_20251130';

    // Add all tables where your ATF creates test data
    var tables = [
        'incident',
        'u_my_custom_table'
    ];

    tables.forEach(function(tbl) {
        var gr = new GlideRecord(tbl);
        gr.addQuery('u_atf_tag', ATF_TAG);
        gr.query();

        var count = 0;
        while (gr.next()) {
            gr.deleteRecord();
            count++;
        }

        gs.info('Deleted ' + count + ' records from ' + tbl + ' with tag ' + ATF_TAG);
    });

})();

 

 

If you don’t have a tag field, you can also delete based on sys_created_by (e.g., your ATF service account), but this is less safe:

var gr = new GlideRecord('incident');
gr.addQuery('sys_created_by', 'atf_user'); 
gr.deleteMultiple();

 

Best practice:
When creating test data in ATF (via server-side script step), set a tag like:

gr.u_atf_tag = 'ATF_RUN_20251130';

 

This makes cleanup reliable and avoids accidental deletion of non-test records.

 

Hope this helps!
If it resolves your query, please mark the answer as correct.

kaushal_snow
Giga Sage

@ServiceNow Use6 ,

 

In ATF by design most test created records are rolled back automatically after the test run, so unless you’ve explicitly excluded the table from rollback (via adding excludeFromRollback=true on the table  dictionary entry) you won’t need a custom delete test data fix script, and if you did exclude it you would instead write a standard server side background script (or fix script) using GlideRecord.deleteMultiple() (or looping gr.delete()) with a proper encoded query to clean up dummy data .........doing that in a non production environment ensures you clean up safely without interfering with atf rollback behavior....

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Technical Consultant - Rising Star/Class of Legends 2025