DataTable - Retain Datable structure but delete all rows



Hello Everyone,

I would like to retain the datatable(dt2) but delete the contents of it. Actually am using for loop to enter data into this table. Once the data is transferred to excel, I would like to delete all the rows in datatable but retain an empty datatable so that I can enter new set of data again. This process is like a loop

Enter data in dt2—> Delete data in dt2—>Enter data in dt2—>Delete data in dt2…goes on like this

I encountered an error when I tried to use Remove Data Row, as I can’t iterate within the same table(dt2) while removing the rows.

Is there any command like dt2 = dt2.clear

Any help would be appreciated



Do you have any reason to stick to one datatable variable?

Use another one and DataTable.Clone method help you to copy structure.


@Uemoe. Thanks for the response. Once I copy the data from datatable to excel, I don’t need that data in datatable anymore. So I would want to use the same datatable to fill in with second set of data. If I have to create new datatable for every set of data, wouldn’t it be cumbersome? Any inputs is appreciated. I am new and not familiar with Uipath


Dotnet has Garbage Collector.

If you don’t need data in datatable it’s mean you want clear memory from its content?
If directly delete data in datatable is not mean you clear memory.

I think in your case best approach is something like that:

newDt = dt2.clone
dt2 = Nothing

After that Garbage Collector will be clear memory for you.

Found link on this topic