I was recently tasked with cleaning up a legacy Drupal 7 database to which had accumulated a lot of data in several fields. The fields were no longer used, and the data could be deleted. The data however totalled more than 30 GB with those 7 fields, and this presented a few challenges.
Existing solutions that did not quite cut it
I search around the internet to see the best way to delete fields in Drupal 7 using an update hook. I came across this stack overflow post to which advised using something like:
The main issue with the above code is that it only marks the data for deletion, and then slowly removes it on cron. This could take weeks to fully remove the data. It was not an ideal solution in our case.
If you want to expatiate the process of removing data from fields, and also the fields themselves, we found the code below would remove the 30 GB of data, remove the tables, update the content types, all in one nice update hook. It does rely on having removed the fields from your features, so that are the respective content type can be reverted successfully.
A couple of lessons can be learnt here
- Truncations, even on large data sets, are extremely quick, and awesome. The above update hook takes less than 1 minute to run end to end.
- If you are going to programmatically update nodes with a high degree of frequency, consider disabling revisions on those content types. Your database thanks you in advance.
If you have had to delete fields in update hooks in Drupal, let me know if you used another method and how that went for you.