One common task in Linux is to delete all text in a file using the Vi/Vim editor. Whether you’re clearing a log, resetting a config file, or preparing a test dataset, knowing the right commands saves time and prevents mistakes.
In this guide, you’ll learn:
- Several methods for wiping a file clean in Vi/Vim
- Why this approach is safer than deleting and recreating the file
- Step-by-step instructions for saving or exiting afterward.
When Do You Need to Delete All Text in a File with Vi/Vim?
Before looking at the commands, it helps to know when clearing a file is better than deleting it entirely. You will often find that emptying a file is a more appropriate solution than deleting it entirely. Here are some common examples:
- Managing Large Log Files: Applications and services constantly write to log files, which can quickly grow large. Clearing them is safer than deleting the file, since active processes may still rely on it. For long-term management, see our Logrotate Troubleshooting Guide.
- Resetting Temporary or Cache Files: When working with programs that use temporary or cache files, you may need to clear them out before running a process. This ensures that you start with a clean slate and that old data does not interfere with the new operation.
- Resetting Configuration Files: Sometimes, you may need to reset a configuration file by clearing its contents before pasting in a new configuration. If you’re working with web server configs, see our guide on configuring NGINX log rotation for related best practices.
- Preparing for Development and Testing: In a development environment, it is common to run tests that read from or write to specific data files. Clearing these files between each test run allows us to ensure that the results of one test do not influence the outcome of the next.
Why Not Just Delete and Recreate the File in Linux?
Deleting and recreating a file might seem simpler, but it often causes problems. First, deleting a file resets its ownership and permission settings, which you would then need to reconfigure manually.
Additionally, this action will break any symbolic or hard links pointing to the original file. Most importantly, if another application is actively using the file, deleting it can cause that application to crash or behave unexpectedly. For these reasons, editing the file in place to clear its contents is a much safer and more reliable practice.
Suggested read: How to edit files using Nano
How to Create a Sample File to Practice in Vi/Vim
To help you follow along with the examples in this guide, you can create a temporary practice file filled with random data. Please open your terminal and execute the following command. This will create a file named tmp.abc in the /tmp/ directory, which you can use to test the commands safely.
tr -dc '[:alnum:] \n' < /dev/urandom | head -c 1024 > /tmp/tmp.abc

How to Delete All Text in a File Using Vi/Vim Commands
Vi/Vim is a powerful text editor, and deleting all text in a file is a straightforward process once you know the right commands. Here are a few of the most effective methods:
Method 1: Delete All Text in Vi/Vim with ggdG
This method combines three Vim commands into one sequence that’s easy to remember. Let’s break it down into its individual components.
- The first part of the sequence, gg, is a command that instantly moves your cursor to the very first character on the first line of the file.
- Next, the d key acts as the delete operator. By itself, d does not delete anything; instead, it signals to Vim that you intend to perform a deletion and waits for a subsequent motion command to define the range of text that should be removed.
- The final part of the sequence, G, provides the motion command that the d operator was waiting for. When used alone, the G command moves the cursor to the very last line of the file.
When you type these commands together as ggdG, the editor is directed to “delete from the first line to the last line,” removing every line in the current buffer.
Step-by-Step: Using ggdG to Clear a File
Here is a detailed, step-by-step guide on how to use this command to clear a file’s content.
- Open Your File in Vi/Vim: First, launch your terminal and open the desired file by typing vim your_file.txt. Make sure to replace your_file.txt with the actual path and name of your file.
- Ensure You Are in Normal Mode: Once the file is open, you must be in Normal mode to execute commands. This is the default mode when you first open a file. If you have been typing or are otherwise unsure of your current mode, simply press the Esc key to return to Normal mode.
- Execute the Deletion Command: With Normal mode active, carefully type the command sequence ggdG in succession. You do not need to press the Enter key. As soon as you type the final G, Vim will immediately execute the full command, and all text within the file will be deleted, leaving you with a blank screen.
- Enter Insert Mode to Add New Content: Now that the file is empty, you can begin adding new content. To do this, press the i key to switch from Normal mode to Insert mode. You will notice that your cursor may change shape, and you can now start typing text as you would in any standard editor.
- Save and Close the File: Once you are satisfied with your changes, you will need to save the file. The section below provides detailed instructions on the various commands used to save your work and exit the Vim editor.

Method 2: Delete All Text in Vi/Vim with :%d
You can also use Vim’s Command-line mode, also known as Ex mode. To delete everything, you can simply type :%d
.
To see how it works, let’s look at each part of the command.
- Typing the colon (
:
) immediately shifts Vim from Normal mode into Command-line mode. This action moves the cursor to the bottom of the screen, which indicates that Vim is now ready to accept a typed command. - The percentage symbol (
%
) is a special character that represents the entire file. - Finally, the
d
character is the command to be executed across the specified range. In Command-line mode, d is the direct command for “delete”.
Step-by-Step: Using :%d
to Clear a File
Here is a detailed, step-by-step guide on how to use the :%d command to clear all text from a file.
- Open Your File in Vi/Vim: To begin, you will need to open the file you wish to modify by executing the following command in your terminal: vim your_file.txt.
- Ensure You Are in Normal Mode: Before you can enter a command-line instruction, make sure you are in Normal mode. If you have just opened the file, you are already in this mode. However, if you have been editing or are uncertain, simply press the Esc key to ensure you return to Normal mode.
- Enter Command-line Mode and Execute the Command: Now, type a colon (:). You will see the cursor jump to the bottom-left corner of your screen, which indicates that Vim is now in Command-line mode. Next, you need to type %d after the colon and then press the Enter key to confirm and execute the full command. The entire content of the file will be deleted instantly.
- Enter Insert Mode to Add New Content: With the file now empty, you can switch into Insert mode to begin adding your new text. Press the i key to enable insert mode. This will allow you to type freely within the file.
- Save and Close the File: After you have finished typing your new content, you will need to save your changes and exit the editor. Please refer to the section below to learn how to save and close the file with Vim.

How to Save and Exit After Deleting Text in Vi/Vim
After clearing a file, the changes exist only in Vim’s memory. To make them permanent, you need to save (or “write”) the buffer to disk.
Save Without Exiting in Vi/Vim (:w)
There may be situations where you want to save your progress but continue working within the file. The :w
command allows you to do just this.
- First, press the Esc key to ensure you are in Normal mode.
- Type :w and then press the Enter key.
- Vim will save the file, and you will see a confirmation message at the bottom of the screen with the file name and the number of lines and characters written. After you execute this command, you will remain inside the editor and this allows you to continue your work.

Save and Quit in Vi/Vim (:wq or ZZ)
This is the most frequently used command when you have finished your work and wish to save it and close the editor in a single step.
- The
:wq
command is a combination of the “write” command (w) and the “quit” command (q). It first saves the file and then exits the Vim editor. - An even faster alternative is the
ZZ
command (note the capital letters). This is a Normal mode shortcut that performs the exact same action as:wq
. It saves the file if it has been modified and then quits the editor.
How to use :wq in Vim:
- Press the Esc key to enter Normal mode.
- Type
:wq
and press the Enter key to save your changes and exit.

How to use ZZ in Vim:
- Ensure you are in Normal mode by pressing the Esc key.
- Without typing a colon, simply type the capital letters ZZ in succession. The editor will immediately save the file and close it.
Quit Without Saving in Vi/Vim (:q!
)
If you accidentally delete the file’s content and want to revert, then you can close the file without saving. The q stands for “quit,” while the exclamation mark (!) is a “force” modifier. By adding !, you are telling Vim to quit immediately and to discard all changes made since the last time the file was saved.
How to use it:
- Return to Normal mode by pressing the Esc key.
- Type
:q!
and press the Enter key. - Vim will close instantly without saving.

Final Thoughts: Easier File Management with RunCloud
You’ve seen how easy it is to delete all text in a file using Vi/Vim with commands like ggdG or:%d. Once you know them, these commands are quick and give you full control from the terminal. If your goal is freeing up storage, our guide on checking disk space in Linux shows how to spot problems before they cause downtime.
Still, managing files directly from the terminal takes time and leaves room for error. That’s where modern server management tools can make a difference.
With RunCloud, you get both SSH access and a browser-based file manager. That means you can edit, clear, and manage files in seconds — without needing to remember Vim shortcuts.
Our platform gives you a secure, browser-based file manager alongside full SSH access, so you can edit, clear, and manage files in seconds — without memorising Vim shortcuts.
Try RunCloud today and simplify the way you manage files on your servers.
At a Glance: Commands to Delete Text in Vi/Vim
If you just need the key commands in one place, here’s a quick reference you can scan before diving into the FAQs.
Command | What it does | Example usage |
---|---|---|
ggdG | Deletes all text in the file using normal mode | Open file → type ggdG |
:%d | Deletes all text in the file using command mode | :%d then press Enter |
dd | Deletes the current line | Move to a line → type dd |
u | Undoes the last change | Press u after a deletion |
:q! | Quits without saving changes | :q! then press Enter |
:wq | Saves and quits after deleting | :wq then press Enter |
FAQs on Deleting All Text in a File Using Vi/Vim
How do I delete a single line in Vi/Vim instead of all text?
Use the dd
command. Move the cursor to the line you want to delete and press dd. That removes just that one line.
Can I undo deleting all text in Vi/Vim?
Yes. Press u to undo the last change. If you deleted everything with ggdG
or :%d
, pressing u will bring the text back.
What’s the difference between ggdG and :%d in Vi/Vim?
Both commands delete all text in the file. The difference is how you use them. ggdG
is a normal-mode command sequence, while :%d
is an Ex command typed on the command line inside Vim.
How do I delete text in Vi/Vim without saving the changes?
After clearing the file, exit with :q!
. That quits Vim without saving, so the file stays unchanged on disk.
What’s the fastest way to clear a very large file in Vi/Vim?
Both ggdG and :%d
work quickly, even on large files. If the file is extremely big, :%d
is usually a little faster because it runs as a single command.