Importing CSV Files into WordPress: Clean Up

This is the final part in a series for how to Import CSV Files into WordPress.

Over the past few posts, I’ve walked through a strategy that I’ve found to useful and effective when importing large CSV files into WordPress.

Specifically, I’ve broken the approach down into three other articles each of which covers a high-level overview of how to achieve a certain part of the process through a mix of PHP, JavaScript, and pseudo-code.

Up to this point, I’ve covered:

  1. How to setup the script to prevent timeouts
  2. Working to build a back end that supports visual cues on the front end
  3. How to display visual cues on the front use via Ajax

And the last part is by far the easiest: Clean up after yourself.

Cleanly Importing CSV Files into WordPress

If you’ve been following closely with the previous articles, then nothing in this post should come as a surprise, but I’m including it for the sake of completeness and for the sake of making sure that you keep a clean php_error.log when someone uses your work.

Cleanly Importing CSV Files into WordPress
Is an error log an error log if there are no errors in the log?

After all, when something goes wrong with their installation, the last thing you want them to find is a bunch of residual files, records, log files spread throughout their server, right?

So in order to bring this thing full circle, we need to make sure that we’re removing the necessary things that we created when processing our CSV.

1. Partial CSVs

The first thing that you want to remember to clean up are all of the partial CSV files that are created during the first phase of the import.

Remember: We’re breaking a large file into n-number of smaller files so that we can import them sequentially to avoid timeout errors. The thing is, if you forget to actually delete the file after importing it, then you may run into problems with future imports.

For example, perhaps you’re only importing two files when four files exist. What happens then? Have you considered handling pre-existing records or handling duplicates?

All of the above are importing considerations but the primary point is that you need to make sure that for each file that you write, you delete, as well.

2. Options

At least part of the process that’s involved in importing a large CSV file is rooted in saving options (as they are called in WordPress), transient data, or cache values.

Wherever you opt to save them doesn’t matter.

As long as you remember to delete the options once your process is complete, then you’re good to go. Remember, a number of the options that we’re saving are used to help calculate the percentage of progress the process has made in importing the files.

Forget to remove those options, or don’t properly write to them to begin with and your values could be skewed from every import after the first one.

3. The Timer

Though this isn’t technically a file or a record, it’s worth mentioning any time you’re using a timer in JavaScript (specifically via setInterval), it’s important to make sure that you clear the timeout once the process has completed.

Otherwise, the browser will continue to poll the server which will continue to return the response that the import is complete and you end up just bogging down the browser more than necessary.

So remember to pass a reference of your timer to clearInterval() once your process has completed. That way, you’ve cleaned up the files, the records, and the browser is no longer doing anything to poll the server only the get the same response.

The End

And that’s it.

Sure, there’s likely a lot of gaps that I didn’t feel when working on this, but the point wasn’t to provide a comprehensive overview of the topic – instead, it was meant to provide a high-level overview of how you may go about achieving this.

In short:

  • Break the file into smaller files
  • Read each file one at a time
  • Save database records to track progress
  • Clean up after yourself

Of course, it’s a bit easier than it sounds, right?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.