{"id":1766,"date":"2010-09-29T12:00:07","date_gmt":"2010-09-29T06:30:07","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1766"},"modified":"2016-12-19T15:04:07","modified_gmt":"2016-12-19T09:34:07","slug":"gorm-batch-deletes-made-easy-with-load-method","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/gorm-batch-deletes-made-easy-with-load-method\/","title":{"rendered":"GORM Batch Deletes Made Easy with Load Method"},"content":{"rendered":"<p>We are using the <a href=\"http:\/\/www.grails.org\/plugin\/asynchronous-mail\" target=\"_blank\">grails asynchronous mail plugin<\/a> in our project and noticed that the sent mails never gets deleted from the database. This was an issue because we were sending out a lot of mails and all of them had PDF attachments of about 500 KB to 1 MB each. A sure recipe for disaster because our DB was growing exponentially. To take care of this, we decided to do a DB cleanup and delete the messages that had the status SENT.<\/p>\n<p>Going through the documents for Deleting Objects, we noticed that grails doesn&#8217;t really support batch deletes. The executeUpdate() method, which was provided there as a workaround, was also not a suitable solution because we would&#8217;ve had to delete the associations explicitly. This is because executeUpdate() wasn&#8217;t doing anything with the associations, which in our case where the attachments.<\/p>\n<p>We found a suitable solution after going through <a href=\"http:\/\/www.tothenew.com\/blog\/grails-load-proxy-domain-objects\/\" target=\"_blank\">Amit&#8217;s blog about loading proxy objects<\/a>. What we did was something like this.<\/p>\n<p>[java]<br \/>\ndef sentMails = Mail.findAllByStatus(&amp;quot;SENT&amp;quot;)<br \/>\nsentMails*.discard() \/\/detach all the objects from session<br \/>\nsentMails.each{<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Mail proxySentMailObject = Mail.load(it.id) \/\/load proxy object<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 proxySentMailObject.delete() \/\/using the proxy object to delete from DB<br \/>\n}<br \/>\n[\/java]<br \/>\nThis also ensures that we don&#8217;t face the ConcurrentModificationException we would have faced if we tried deleting directly using<br \/>\n[java]<br \/>\nsentMails.each{<br \/>\n       it.delete()<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Special Thanks to <a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit\/\" target=\"_blank\">Amit<\/a> for the wonderful post which was of great help!<\/p>\n<p>Hope this helps<br \/>\nVivek<br \/>\nvivek[at]intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are using the grails asynchronous mail plugin in our project and noticed that the sent mails never gets deleted from the database. This was an issue because we were sending out a lot of mails and all of them had PDF attachments of about 500 KB to 1 MB each. A sure recipe for [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[7],"tags":[419,29,4840,420],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1766"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1766"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1766\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}