To delete every line containing a string in multiple files, run a regex find and replace across the folder with the pattern ^[^\r\n]*word[^\r\n]*\r?\n (multiline mode on) and an empty replacement. The pattern matches the whole line plus its line break, so the line disappears completely instead of leaving a blank gap. In PowerShell, the equivalent is to keep only lines that do not match: Where-Object { $_ -notmatch 'word' }.
This guide explains the pattern piece by piece, shows variations for deleting lines that do not contain a word, and covers the edge cases that leave stray blank lines or miss the last line of a file.
Typical reasons to delete lines in bulk
- Removing debug statements such as
console.log(orprint("DEBUGfrom a codebase before release. - Stripping an old tracking script, meta tag or stylesheet link from every page of a site.
- Deleting a setting from hundreds of config files.
- Cleaning log or data files of noise lines, such as health-check requests.
The pattern, explained
^[^\r\n]*console\.log\([^\r\n]*\r?\n
| Part | Meaning |
|---|---|
^ | Start of a line (needs multiline mode) |
[^\r\n]* | Anything on the same line before the word |
console\.log\( | The text to find, with the dot and bracket escaped |
[^\r\n]* | The rest of the line |
\r?\n | The line break, Windows (CRLF) or Unix (LF) |
Using [^\r\n]* instead of .* keeps the match on one line in every engine, even if a "dot matches newline" option is switched on. Replace with nothing.
If the word could be part of other words, add boundaries: ^[^\r\n]*\bDEBUG\b[^\r\n]*\r?\n removes lines with DEBUG but keeps lines with DEBUGGER.
The last line of the file
If the final line of a file contains the word and has no line break after it, the pattern above will not match it. To include it, allow the end of the text as an alternative:
^[^\r\n]*word[^\r\n]*(?:\r?\n|\z)
In engines without \z, such as JavaScript in VS Code, run a second pass for \r?\n[^\r\n]*word[^\r\n]*$ without multiline mode, which removes a matching final line together with the break before it.
Variations
| Goal | Find (multiline mode) |
|---|---|
| Delete lines containing any of several words | ^[^\r\n]*(?:foo|bar|baz)[^\r\n]*\r?\n |
| Delete lines starting with a word | ^\s*#[^\r\n]*\r?\n (comment lines starting with #) |
| Delete lines that do NOT contain a word | ^(?![^\r\n]*keep)[^\r\n]*\r?\n |
| Delete lines containing a word, plus the next line | ^[^\r\n]*word[^\r\n]*\r?\n[^\r\n]*\r?\n |
| Delete only the word, keep the line | \s*\bword\b (no line break in the pattern) |
The "do NOT contain" pattern uses a negative look-ahead. It also removes empty lines, because they do not contain the word either. If that is not what you want, use ^(?![^\r\n]*keep)[^\r\n]+\r?\n so only non-empty lines are removed. For more on look-aheads, see our regex cheat sheet.
Test the pattern on sample lines first
Before running a delete across a folder, paste a few representative lines into a scratch file and try the pattern there. Include:
- A line that should be deleted, with the word in the middle.
- A line with the word at the very start and one with it at the very end.
- A line with a similar but different word, such as DEBUGGER when you are deleting DEBUG.
- An indented line, and a line that is the last line of the file with no line break after it.
Each one should behave as you expect. If the similar word is deleted too, add \b boundaries. If indented lines survive, check that nothing in your pattern requires the word to be at the start of the line.
Comment out instead of delete
If you might need the lines again, comment them out rather than removing them. In JavaScript, find ^([ \t]*)(console\.log\() with multiline mode on and replace with $1// $2. The first group keeps the indentation. Later, a single rule can remove all the commented lines, or restore them by reversing the pattern.
Method 1: PowerShell
For one file:
(Get-Content app.js) | Where-Object { $_ -notmatch 'console\.log\(' } | Set-Content app.js
For every .js file in a folder tree:
Get-ChildItem -Recurse -File -Filter *.js | ForEach-Object { $lines = Get-Content $_.FullName -Encoding UTF8; $keep = $lines | Where-Object { $_ -notmatch 'console\.log\(' }; if ($keep.Count -ne $lines.Count) { Set-Content $_.FullName $keep -Encoding UTF8 } }
Notes:
- The brackets around
Get-Contentin the one-file version make PowerShell read the whole file before writing back to it. -notmatchis a regex and ignores case. Use-cnotmatchfor case-sensitive matching.- Working line by line,
Set-Contentwrites Windows (CRLF) line endings and adds a final line break, which can change LF files. If that matters, use the regex approach on the whole file withGet-Content -Rawinstead. - Back up first. There is no undo.
Our full guide to replacing text in multiple files with PowerShell covers encodings and backups.
Method 2: an editor
- Notepad++: open Find in Files (Ctrl+Shift+F), choose Regular expression mode, find
^.*word.*\R, leave Replace with empty, set Directory and Filters, and click Replace in Files. In Notepad++ the dot does not match line breaks unless you tick ". matches newline", and\Rmatches any line ending. For a single open file, you can also use Search → Mark with Bookmark line ticked, then Search → Bookmark → Remove Bookmarked Lines. - VS Code: in the Search view (Ctrl+Shift+H), turn on regex, find
^.*word.*\nand replace with nothing. VS Code handles Windows line endings when\nappears in a search pattern.
Method 3: a batch tool with preview
Deleting lines is unforgiving: once a line is gone, nothing in the file hints that it was ever there. That makes a preview and an undo especially valuable. In Bulk Replace, add a regex rule with the line pattern and an empty replacement, choose the folder and file types, and check the before/after diff. Each removed line is shown in every file, so you can spot a line that matched by accident and untick it. Bulk Replace backs up every file before it changes, and one click restores the whole run.
Check for leftovers and side effects
- Search for the word again. Any remaining matches are usually on the final line of a file, or on lines where the text is written differently (extra spaces, different case).
- For code, run the build or tests. A deleted line can leave an empty block or a dangling comma.
- If you only need to remove blank lines left over from an earlier edit, see how to remove empty lines from multiple files.
Frequently asked questions
What regex deletes a whole line containing a word?
Use ^.*word.*\r?\n with multiline mode on and replace with nothing. The \r?\n removes the line break too, so no blank line is left behind.
How do I delete lines that do not contain a word?
Use a negative look-ahead: ^(?!.*word).*\r?\n with multiline mode on, replaced with nothing.
Can PowerShell delete lines containing text?
Yes. (Get-Content file.txt) | Where-Object { $_ -notmatch 'word' } | Set-Content file.txt keeps only the lines that do not match.
