What is the name of this action anime involving magic that I had watched about a couple of years ago?
The anime you are looking for is Kannagi, the events you described are from episode 2. The purple-haired girl is either Nagi or her sister.




  Our unlucky protagonist, Jin, uses the trunk of a sacred tree to carve a statue for a school project. When he takes it outside, to his surprise it begins absorbing the surrounding earth and transforms into, hold your breath on this one, a girl! So like all similar setups this guardian deity is pretty pissed that her tree was cut down and lives with Jin while she takes out her anger on squashing bugs....er, cleaning the "Impurities."