How I Deleted 100000 Comments!

May 23, 2011

Finally, no spam! My blog was slowly becoming dysfunctional, after getting more than 2000 spam comments per day!

I moved to the new version 2.0 of last night, and it has super easy built in reCAPTCHA which stops spam and helps read books. Pretty cool! The reCAPTCHA settings are accessed by hovering over Tools and selecting Edit in .NET Blog Engine 2.0 – it was very hard and non-intuitive to find the setting, but once I did it worked like a charm!

There was one "small glitch" though – about a hundred thousand spam comments left to "moderate", which I did.


peace Now I feel at peace like the picture on the left

I made a small program, which anyone can use to mass-delete blog engine’s comments after specific date. Hopefully this will help anyone unspam themselves.

private void button1_Click(object sender, RoutedEventArgs e)
    string[] files = Directory.GetFiles(@"D:\TEMP\blog\App_Data\posts", "*.xml");
    foreach (string file in files)

private void FixPost(string fileName)
    XDocument doc;
    using (FileStream stream = File.OpenRead(fileName))
        doc = XDocument.Load(stream);

    var list = from XElement el in doc.Descendants(XName.Get("comment", String.Empty)) 
               select el;

    // convert the list to an array so that we're not modifying the collection that we're iterating over    
    XName date = XName.Get("date");
    foreach (XElement el in list.ToArray())
        string data = ((XElement)el.FirstNode).Value;

        if (data.StartsWith("2011") || data.StartsWith("2010-12") || data.StartsWith("2010-11") || data.StartsWith("2010-10"))

    using (XmlWriter writer = XmlWriter.Create(fileName))

That code deleted all comments from Oct 2010 onward (sorry, I had to do it), but yay! No spam! I can finally blog normally. Smile

Hope you like it! Please comment in a nice and clean environment! Smile

blog comments powered by Disqus | Terms | Log in


About the author

Happy & enjoying life. Software enthusiast.
The opinions I express here and on are mine and not my employeer's (Microsoft).
This is the official blog of You can find Silverlight samples, coding stuff, and hopefully other interesting things here.