Clearing a jQuery DataTable usually takes just one line of code: call the table’s clear() method, then call draw() to update what appears on the page. For example, $('#myTable').DataTable().clear().draw(); removes the rows from a client-side DataTable while keeping the table structure and its DataTables features intact.
The right approach depends on how your table gets its data. A client-side table can be cleared directly, while an Ajax or server-side table may be repopulated when it reloads. This guide explains how to clear a DataTable in jQuery, when to use each method, and how to avoid common problems such as duplicate initialization and rows unexpectedly returning.
The Quick Way to Clear a DataTable
If your DataTable is already initialized and its data is managed in the browser, use the DataTables API:
const table = $('#myTable').DataTable();
table.clear().draw();
The clear() method removes all data rows from the DataTable’s internal data set. The draw() method refreshes the table display so the cleared state appears immediately. You can also chain the methods:
$('#myTable').DataTable().clear().draw();
This clears the rows without removing the table element, column headings, search box, pagination controls, or other DataTables configuration. It is generally the best solution when you want the existing table to remain available for new data.
What the clear() and draw() Methods Do
DataTables keeps track of table data internally. Because of that, removing row elements directly from the page does not reliably update the DataTables data set. The API methods clear() and draw() work with that internal data and then refresh the visible table.
-
clear(): Removes the rows from the DataTable’s internal data set. -
draw(): Redraws the table so changes are reflected in the browser.
For most situations, use them together. Calling clear() by itself may not immediately update the visible display. Calling draw() afterward applies the change and updates related interface elements, such as the pagination information.
Clearing the rows does not destroy the DataTable. Its initialization, columns, event handlers, and configuration remain in place, so you can add new data later.
Clear a DataTable When a Button Is Clicked
To let a user clear a table, attach a click handler to a button. Initialize the DataTable once, then use the saved instance in the handler:
<button id="clearTable">Clear table</button>
<table id="myTable">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script>
$(function () {
const table = $('#myTable').DataTable();
$('#clearTable').on('click', function () {
table.clear().draw();
});
});
</script>
Saving the instance in a variable makes it easy to use the same DataTable throughout your code. It also helps prevent accidental repeated initialization, which can trigger warnings or create unpredictable behavior.
Clear a DataTable Before Adding New Rows
If you are replacing the current data with a new set of rows, clear the table, add the new data, and draw once at the end. The rows.add() method accepts an array of row data:
const table = $('#myTable').DataTable();
const newRows = [
['Ava', 'ava@example.com'],
['Noah', 'noah@example.com']
];
table.clear();
table.rows.add(newRows);
table.draw();
Calling draw() after the update makes the change visible. Drawing once after both clearing and adding can also avoid unnecessary redraws when you are replacing a large number of rows.
The data format must match your table’s column configuration. For a table configured with objects, add an array of objects instead:
table.clear();
table.rows.add([
{ name: 'Ava', email: 'ava@example.com' },
{ name: 'Noah', email: 'noah@example.com' }
]);
table.draw();
For object data, configure the columns to use the corresponding property names, such as name and email.
Clearing an Ajax-Powered DataTable
If a DataTable loads rows through Ajax, calling clear().draw() removes the rows currently held by the browser. However, a later Ajax reload can bring the rows back because the data source still contains them.
To reload the table from its Ajax source, use ajax.reload():
const table = $('#myTable').DataTable();
table.ajax.reload();
This requests fresh data from the configured endpoint. If you want the refreshed table to keep its current page where possible, pass null as the callback and false as the second argument:
table.ajax.reload(null, false);
If your goal is to display an empty table after a user deletes records, make sure the Ajax endpoint returns the updated data. Clearing only the browser-side rows does not delete records from your database or change what the server returns. Perform the appropriate server-side update, then reload the DataTable.
Clearing a Server-Side DataTable
When serverSide: true is enabled, DataTables relies on the server to provide rows for each draw. In this mode, clear() is not the right way to permanently empty the table. A draw can request data again, and the server may return the same records.
To make a server-side DataTable empty, update the data at its source. For example, delete or archive the relevant records through your application, or change the server-side filter so the query returns no rows. Then reload the table:
$.ajax({
url: '/records/clear',
method: 'POST'
}).done(function () {
table.ajax.reload();
});
The endpoint in this example is application-specific. It should carry out the appropriate authorized action and return a successful response. If you only want to show a different result set, update the table’s search or filter criteria and reload it rather than trying to clear the client-side rows.
Clear Rows Without Destroying the DataTable
Clearing data and destroying a DataTable are different actions. Use clear().draw() when you want to remove rows but keep the existing table instance and its features. Use destroy() when you need to remove DataTables enhancements, change the table structure, or initialize the same table again with different settings.
For example, if you are changing the columns themselves, you may need to destroy the existing instance before changing the table markup and creating a new one. For simply removing the current rows, destroying and reinitializing is unnecessary and can introduce extra work.
Also, do not call destroy() just to make the table appear empty. It does not mean “clear the data and keep using the same initialized table.” Choose the API method that matches the actual change you want to make.
Common Mistakes to Avoid
-
Removing rows with jQuery alone: Code such as
$('#myTable tbody').empty()changes the page markup without properly updating DataTables’ internal data. Use the DataTables API instead. -
Forgetting to draw: After calling
clear(), calldraw()to refresh the display. - Expecting a database delete: Clearing a browser table does not remove records from your server or database.
- Clearing a server-side table only in the browser: The next server request may return the same rows. Update the source data or server-side filter, then reload.
- Initializing the table repeatedly: Store the DataTable instance and reuse it. If you must initialize conditionally, check whether it is already initialized before creating another instance.
-
Using the wrong row format: Make sure arrays or objects passed to
rows.add()match the table’s column configuration.
Keeping changes inside the DataTables API is especially important when sorting, paging, filtering, or Ajax loading is enabled. Those features depend on the table’s internal state, not only on the HTML currently visible in the browser.
Frequently Asked Questions
Does clear() remove the table headings? No. It removes data rows while leaving the table and its headings in place.
Can I add rows again after clearing? Yes. Use rows.add() to add new data, then call draw() to display it.
Why do my rows return after I clear the table? The table may be loading data from Ajax or using server-side processing. When it reloads, the source can send the rows again. Update the source data or adjust the server-side query before reloading.
Do I need to destroy and reinitialize the table? Not to remove rows. Use clear().draw() for that. Destroy and reinitialize only when you need to change the table setup in a way that requires a new instance.
Conclusion
To clear a client-side DataTable in jQuery, call table.clear().draw(). This removes the data rows while preserving the table’s configuration and controls. If you are replacing the data, add the new rows before drawing. If the table uses Ajax or server-side processing, update or filter the data source and reload the table so cleared records do not return. Using the DataTables API instead of directly removing HTML rows keeps the table’s internal state in sync and helps prevent display and pagination issues.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.