Performant way to clear excessive old data in a text file

how to delete millions of records in sql server
sql server bulk delete performance
sql server fastest way to delete records
sql server automatically delete old records
streamwriter clear file before writing c#
how to empty a file in unix without deleting it
sql server purge old data
sql improve delete performance

I have some text files that I need to be able to write to on the fly, and quite often. These files can get pretty big in size, but the text inside them can be completely different between two iterations.

This means that text can be shorter than it previously was. Which, if i wouldn't clear the old data first, would end up in a mix of the old data being appended at the end of my new data. As indicated by the MSDN docs.

If you overwrite a longer string (such as "This is a test of the OpenWrite method") with a shorter string (such as "Second run"), the file will contain a mix of the strings ("Second runtest of the OpenWrite method").

However the docs do not specify a way to remedy or even prevent this from happening.

Currently i'm doing the following:

File.WriteAllText(path, string.Empty);
using (Stream file = File.OpenWrite(path))
    file.Write(dataToWrite, 0, dataToWrite.Length);

Where i empty the contents of the existing file using File.WriteAllText(path, string.Empty) and then write the new contents to the file.

However it feels like a waste to have to go over the entire file twice (First to clear it, and then to write the new data to it).

Is there a way where i can overwrite the old data with my new data, and only go over the "left over" data and clear that, without going over the entire file twice?

It doesn't necessarily have to use Stream.Write. Any alternative that gets the job done and is faster is acceptable.


After running 100.000 iterations of writing 2441 kb of data (and clearing all old data) 5 times on different machines, the following results came out:

  • My original method found above took 4.75589 ms on average.
  • Anderson Pimentel's answer using WriteAllBytes took 4.28946 ms on average.
  • fastest Dark Falcon's answer using file.Write and truncating took 4.14433 ms on average (and is the fastest/most consistent with this).
  • File.Delete the old file and creating a new file using FileStream.Write took 5.31883 ms on average.
  • MeJustAndrew's answer doing the above but multithreaded took 8.12726 ms on average. (Though i have to admit this could very well be because of poor implementation by me, I am in no way very experienced in multi threading)

note that these results apply to my implementation and hardware. Results may vary on different hardware.

I would recommend truncating the file:

using (FileStream file = File.OpenWrite(path))
    file.Write(dataToWrite, 0, dataToWrite.Length);

You should test whether this performs better than writing a new file, deleting the old file, and renaming the new file to the old name.

Deleting Historical Data from a Large Highly Concurrent SQL Server , When I was asked to remove historical data from a 120 GB unpartitioned In order to generate a text file with the DELETE statements we need to a guide on how to Install SQL Server 2012 Performance Dashboard Reports. Delete unnecessary files. Once you have selected the files you wish to delete, select "Ok." This may prompt a window to appear that will confirm your actions.

You could just write:

File.WriteAllBytes(path, dataToWrite);

According to MSDN:

Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.

Remove all previous text before writing, You could just write an empty string to the existing file: File.WriteAllText(@" Default\DefaultProfile.txt", string.Empty);. Or change the second  A quick way to clear formatting with Format Painter; How to remove only specific formats; How to clear all formatting in Excel. The most obvious way to make a piece of information more noticeable is to change the way it looks. Excessive or improper formatting, however, may have an opposite effect, which makes your Excel worksheet difficult to read.

As I mentioned, a multithreaded approach would look like this:

class FileWriter
    private int index;
    private string fileName = "file.txt";
    private readonly object obj = new object();
    private string FileName { get { lock (obj) { return fileName + index; } } }

    public void Write(string content)
        lock (obj)
            int deleteIndex = index;
            new Thread(() => DeleteOldFile(deleteIndex)).Start();
            new Thread(() => File.WriteAllText(fileName + index, content)).Start();

    public string GetFileContent()
        lock (obj)
            return File.ReadAllText(FileName);

    private void DeleteOldFile(int fileNumber)
        var fileToBeDeleted = fileName + fileNumber;
        if (File.Exists(fileToBeDeleted))

Note: I don't guarantee for the correct behavior of this code, as I have not tested it.

Top 3 Ways to Clean Temporary Files From Your Computer, Excessive clutter can limit your PC's performance in two key ways. Temporary setup and program files; Old Chkdsk files; Setup logs; Windows Update & Windows upgrade leftover files; Windows Defender Type in the following text: Cached files: Temporarily stored data of recently accessed websites. Step Two: Delete your old text messages Whenever you send a text message with a picture that picture has to be stored somewhere. That somewhere is your Other memory. Sadly there is no easy way to

InfoWorld, Sof tware Reviews File Compressor Crams Data Into Smaller Space, of ending and starting pages, with the heavy- handed menu system of Microsoft Word, 2 Ln 15 Pos you locate, save, insert, copy, append, view, search, or delete text files. letting you sort lines or paragraphs with multiple keys defined in many ways. After saving that portion of the data, use the clear function to remove the variable from memory and continue with the data generation. Clear Old Variables from Memory When No Longer Needed When you are working with a very large data set repeatedly or interactively, clear the old variable first to make space for the new variable.

5 Ways to Empty or Delete a Large File Content in Linux, NEC's crisp, clear, high-performance JC1 203 RGB color monitor, an industry standard. and printer-configuration data and password information from files on disk. These names and passwords for the users are also stored in a text file. new 8086/8088 processor is more sophisticated than the older ASM assembler. As Ron mentioned, one of reasons for increase in file size may be pivots, apart from that the issue may occur if : 1. Have blank characters in empty cells 2. Unwanted formatting in cells (example :say you have formatted the entire row/column with a particular color, where in you do not require that in all the cells) Solutions/Workarounds : 1.

Beginning Haskell: A Project-Based Approach, w^&®m,mmmm^m&m^jmmm*mm Program Manager | File Users Groups Servers View of how to use LANDesk to improve your network's performance and reliability. Examples include detecting excessive network traffic, finding chattering NICs, NetWare Btrieve NLM, but it does not make it clear that Btrieve is required. However, the page file that’s stored on the hard drive isn’t. The page file is an area of the physical hard drive that the operating system uses as extra RAM when the physical RAM is full. Ensuring that the page file is cleared just like RAM is will keep things clean and efficient the next time you start your computer.