- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2022 06:06 AM
I want to create a scheduled job to delete all undelivered emails for today. Tried doing this with below script but its not working:
var emailGr = new GlideRecord('sys_email');
emailGr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^sys_idISNOTEMPTY^subject=Undelivered Mail Returned to Sender');
emailGr.query();
var count = emailGr.getRowCount();
while(emailGr.next()){
var grReport = emailGr.sys_id.getRefRecord();
if(grReport.isValidRecord())
grReport.deleteRecord();
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2022 06:11 AM
Hi
Something like below should work. Elevate your privileges and execute below script from background.
var gr=new GlideRecord('sys_email');
gr.addQuery('type','send-ready');
gr.deleteMultiple();
You can also change the type to send-ignored rather than deleting those.
Mark my answer correct & Helpful, if Applicable.
Thanks,
Sandeep

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2022 06:10 AM
Hi there,
Have you considered using a Scheduled Flow instead? Zero code involved.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2022 06:10 AM
You could also consider using the Auto Flush table [sys_auto_flush].
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2023 04:01 AM
Auto flush cannot be used for extensions and sys_email is extension under table rotations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2022 06:11 AM
Hi
Something like below should work. Elevate your privileges and execute below script from background.
var gr=new GlideRecord('sys_email');
gr.addQuery('type','send-ready');
gr.deleteMultiple();
You can also change the type to send-ignored rather than deleting those.
Mark my answer correct & Helpful, if Applicable.
Thanks,
Sandeep