PostgreSQL ts_delete: How to Remove Words from TSVector
In this article
The ts_delete function in PostgreSQL is used to remove a specific lexeme (a normalized word) from a tsvector value. This is primarily used to refine full-text search indexes by removing "noise" words or correcting indexing errors without having to re-index the entire document. To use it, you provide the existing tsvector and the lexeme you wish to delete; the function returns a new tsvector with that specific word removed.
What You'll Need
- A PostgreSQL database (version 10 or newer, as
ts_deletewas introduced in PostgreSQL 10). - A table containing a column of type
tsvectoror a way to cast text totsvectorusingto_tsvector(). - Administrative or write access to the database to perform
UPDATEoperations.
Step-by-Step
- Identify the Lexeme to Remove
Before calling the function, you must know the exact "normalized" form of the word. PostgreSQL does not delete the raw word you typed, but the lexeme produced by the text search configuration. For example, if you want to delete "running," the lexeme is likely "run." Use
to_tsvector('english', 'running')to verify the lexeme before proceeding. - Test the Deletion with a SELECT Statement
Before updating your data, run a
SELECTquery to ensure the function behaves as expected.
SELECT ts_delete(to_tsvector('english', 'the quick brown fox'), 'quick');
The result should be atsvectorcontaining 'brown', 'fox', and 'quick' (if 'quick' was a stop word) or simply excluding 'quick' if it was a searchable lexeme. - Apply the Deletion to a Specific Row
To permanently remove a word from a column in your table, use an
UPDATEstatement.
UPDATE articles SET document_vector = ts_delete(document_vector, 'lexeme_to_remove') WHERE id = 123; - Perform a Bulk Deletion
If you need to remove a specific word from all records in a table (for example, a brand name that should no longer be searchable), run the update without a specific ID filter:
UPDATE articles SET document_vector = ts_delete(document_vector, 'unwanted_word'); - Verify the Results
Run a search query using the
@@operator to ensure the removed lexeme no longer triggers a match for the affected rows.
SELECT * FROM articles WHERE document_vector @@ to_tsquery('english', 'unwanted_word');
This query should return zero results for the rows you updated.
Common Mistakes
- Using Raw Words Instead of Lexemes: The most common error is passing the original word (e.g., "cats") instead of the stemmed lexeme (e.g., "cat"). If the lexeme does not match exactly,
ts_deletewill do nothing and return the original vector without an error. - Forgetting the Configuration: If you are using a non-default language configuration (like 'french' or 'spanish'), ensure you are thinking in terms of that language's stemming rules when identifying the lexeme.
- Assuming it Modifies the Table Automatically:
ts_deleteis a functional operator. It returns a value; it does not modify the table on its own. You must wrap it in anUPDATEstatement to save the changes.
Tips
- Chain Deletions: If you need to remove multiple words, you can nest the functions:
ts_delete(ts_delete(vector, 'word1'), 'word2'). - Use with Triggers: If you have a specific word that should never be indexed, you can incorporate
ts_deleteinto aBEFORE INSERT OR UPDATEtrigger to sanitize thetsvectorautomatically. - Check Stop Words: Before using
ts_delete, check if the word you are trying to remove is already a "stop word" in your PostgreSQL configuration. Stop words are ignored byto_tsvectorautomatically and do not need to be deleted.
Troubleshooting
If the word is still appearing in search results after you have run ts_delete, follow these steps:
- Check the Lexeme: Run
SELECT to_tsvector('english', 'your word');. If the output is different from the string you passed tots_delete, use the output string instead. - Check for Case Sensitivity: While
tsvectoris generally case-insensitive because of the normalization process, ensure you are passing the lexeme in the format produced by the configuration (usually lowercase). - Re-index the Table: If you are using a GIN index on the
tsvectorcolumn, the index usually updates automatically. However, if you suspect corruption, runREINDEX TABLE table_name;. - Verify the Update: Ensure your
UPDATEstatement actually affected rows. Check the "UPDATE X" message returned by the PostgreSQL console to confirm rows were modified.
Frequently Asked Questions
Does ts_delete remove all occurrences of a word?
Yes. A tsvector stores lexemes as a sorted set of unique words with their positions. When you call ts_delete, it removes the lexeme entirely from that vector, regardless of how many times it appeared in the original text.
Can I use ts_delete on a standard text column?
No. ts_delete requires a tsvector type. If your data is in a text or varchar column, you must first convert it using to_tsvector() or cast it, though you cannot "save" the deletion back into a text column without converting the whole vector back to text (which is not recommended).
Is ts_delete faster than re-indexing the whole column?
For a few specific rows or a few specific words, yes. It is much faster to update a few vectors than to regenerate the tsvector for every single row in a million-row table and rebuild the GIN index.
What happens if the word I try to delete isn't in the vector?
The function will simply return the original tsvector unchanged. It does not throw an error or return NULL if the lexeme is not found.
Can I use this to remove stop words that PostgreSQL missed?
Yes. If you find that certain words are skewing your search results but are not included in the default PostgreSQL stop word list, ts_delete is the correct tool to remove them from your index.