Archiving table records once in a month

varunkumar1
Tera Contributor

Hello Team,

 

I have a requirement to Archive records from a table. Archiving should execute on 5th day of every month.

Kindly suggest inputs.

3 REPLIES 3

KanteS
Giga Guru

Hi,

 

U can try any of the below configuration to achieve the scenario;

 

1)ServiceNow Data Archiving 

 use the Data Archiving plugin.

Create an Archive Rule

Define table and condition (e.g., records older than 6 months)

Schedule it to run monthly (5th day) - best for large amount of data

 

2) Flow Designer

Use a Flow with a Scheduled Trigger set to monthly (5th).

Steps:

Scheduled Trigger → Monthly → 5th

Look Up Records (older than 6 months)

Create Record (archive table)

Delete Record

 

3)Scheduled Script Execution

Create a scheduled job under: System Definition → Scheduled Jobs

Script logic:

Query old records

Insert into archive table

Delete from main table

But for large data simple  scripting can impact performance So use batching for large amou of datant of data

 

If this helps, please mark the answer as Solution and give a Like.

Thankyou 

Ankur Bawiskar
Tera Patron

@varunkumar1 

create archive rule and trigger it using script from scheduled job every 5th day of month

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Tanushree Maiti
Mega Sage

Hi @varunkumar1 ,

 

To achieve your requirement, you can create Archive rule and schedule job.

 

Step1: Create Archive Rule:

  1. Navigate to System Archiving > Archive Rules.
  2. Click New.
  3. Fill in the form:
    • Name: Give it a clear name (e.g., "Archive Closed Incidents Monthly").
    • Table: Select the table (e.g., Incident [incident]).
    • Condition: Define the criteria for archiving (e.g., State | is | Closed AND Closed | on | Last 12 months). It is crucial to only archive inactive data to avoid performance issues.
    • Set Active to False: Do not activate it yet.
  4. Click Submit.

 

Step2: Create a Scheduled Job for the 5th Day
Since the default archival job runs daily, you must create a scheduled job to trigger your rule only on the 5th.
Navigate to System Definition > Scheduled Jobs.
Click New.
Select Automatically run a script of your choosing.
Fill in the form:
Name: "Run [Rule Name] on 5th"
Run: Monthly
Day of month: 5
Time: Set a time, preferably during off-hours (e.g., 01:00:00).
In the Script field, paste the following code (replace YOUR_ARCHIVE_RULE_NAME with the actual name created in Step 1):

var ruleName = 'YOUR_ARCHIVE_RULE_NAME';
var gr = new GlideRecord('sys_archive_rule');
if (gr.get('name', ruleName)) {
var archiver = new GlideArchiveRule(gr);
archiver.run();
gs.log("Archive rule " + ruleName + " executed on the 5th.");
}
Click Submit.

 

Step 3: Activate the Archive Rule
Navigate back to System Archiving > Archive Rules.
Open your rule.
Check the Active box.
Click Update.


Best Practices on Archiving your table
Table Limitation: You cannot create an archive rule on tables that already have Table Rotation or Extension enabled.
Archived Table Name: The data is moved to a table with the ar_ prefix (e.g., ar_incident).
Performance: Archiving large amounts of data at once can impact instance performance. Test the rule in a sub-production environment first. 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: